Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #387685

valgrind leak I can't find

From Mark Summerfield <mark@qtrac.eu>
Subject valgrind leak I can't find
Newsgroups comp.lang.c
Message-ID <j8idnQlHTPZXZFv7nZ2dnZfqn_GdnZ2d@brightview.co.uk> (permalink)
Date 2024-08-22 08:41 +0000

Show all headers | View raw


valgrind tells me that I have memory leaks.

Its summary:

valgrind ./cx_test 
==18053== Memcheck, a memory error detector
==18053== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==18053== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==18053== Command: ./cx_test
==18053== 
OK 788/788
==18053== 
==18053== HEAP SUMMARY:
==18053==     in use at exit: 841 bytes in 50 blocks
==18053==   total heap usage: 2,323,773 allocs, 2,323,723 frees, 65,066,770 bytes allocated
==18053== 
==18053== LEAK SUMMARY:
==18053==    definitely lost: 841 bytes in 50 blocks
==18053==    indirectly lost: 0 bytes in 0 blocks
==18053==      possibly lost: 0 bytes in 0 blocks
==18053==    still reachable: 0 bytes in 0 blocks
==18053==         suppressed: 0 bytes in 0 blocks
==18053== Rerun with --leak-check=full to see details of leaked memory
==18053== 
==18053== For lists of detected and suppressed errors, rerun with: -s
==18053== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

The first leak in the detailed report is this:

==18005== 5 bytes in 1 blocks are definitely lost in loss record 1 of 25
==18005==    at 0x48407B4: malloc (vg_replace_malloc.c:381)
==18005==    by 0x49E38E9: strdup (strdup.c:42)
==18005==    by 0x10CAD4: vec_str_tests (vec_str_test.c:77)
==18005==    by 0x109346: main (cx_test.c:26)

vec_str_test.c:77 is:

    vec_str_insert(&v1, 4, strdup("beta"));

The reason I use strdup() is because my VecStr type takes ownership of
the strings it holds.

This is the type's struct:

typedef struct {
    int _size;
    int _cap;
    char** _values;
} VecStr;

Here is the vec_str_insert() function:

void vec_str_insert(VecStr* vec, int index, char* value) {
    assert_notnull(vec);
    assert_notnull(value);
    if (index == vec->_size) { // add at the end
        vec_str_push(vec, value);
        return;
    }
    assert_valid_index(vec, index);
    if (vec->_size == vec->_cap)
        vec_str_grow(vec);
    for (int i = vec->_size; i > index; --i)
        vec->_values[i] = vec->_values[i - 1];
    vec->_values[index] = value;
    vec->_size++;
}

And here is the grow() function it uses:

static void vec_str_grow(VecStr* vec) {
    const int BLOCK_SIZE = 1024 * 1024;
    int cap =
        (vec->_cap < BLOCK_SIZE) ? vec->_cap * 2 : vec->_cap + BLOCK_SIZE;
    char** p = realloc(vec->_values, cap * sizeof(char*));
    assert_alloc(p);
    vec->_values = p;
    vec->_cap = cap;
}

I can't see what I've done wrong.

Back to comp.lang.c | Previous | NextNext in thread | Find similar | Unroll thread


Thread

valgrind leak I can't find Mark Summerfield <mark@qtrac.eu> - 2024-08-22 08:41 +0000
  Re: valgrind leak I can't find Ben Bacarisse <ben@bsb.me.uk> - 2024-08-22 11:40 +0100
  Re: valgrind leak I can't find Bart <bc@freeuk.com> - 2024-08-22 12:01 +0100
    Re: valgrind leak I can't find Thiago Adams <thiago.adams@gmail.com> - 2024-08-22 08:18 -0300
      Re: valgrind leak I can't find Annada Behera <segfault@tilde.green> - 2024-08-22 17:31 +0530
        Naming conventions (was Re: valgrind leak I can't find) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-08-22 15:32 +0200
          Re: Naming conventions (was Re: valgrind leak I can't find) Thiago Adams <thiago.adams@gmail.com> - 2024-08-22 11:01 -0300
            Re: Naming conventions (was Re: valgrind leak I can't find) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-08-23 17:10 +0200
            Re: Naming conventions (was Re: valgrind leak I can't find) James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-23 11:57 -0400
              Re: Naming conventions (was Re: valgrind leak I can't find) Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-08-25 18:55 +0200
                Re: Naming conventions (was Re: valgrind leak I can't find) James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-25 23:47 -0400
        Re: valgrind leak I can't find Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-08-24 11:47 +0000
          Re: valgrind leak I can't find scott@slp53.sl.home (Scott Lurndal) - 2024-08-24 16:48 +0000
            Re: valgrind leak I can't find Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-24 14:04 -0700
  Re: valgrind leak I can't find Ike Naar <ike@sdf.org> - 2024-08-22 11:41 +0000
  Re: valgrind leak I can't find Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-22 15:20 +0000
  Re: valgrind leak I can't find Mark Summerfield <mark@qtrac.eu> - 2024-08-22 17:44 +0000
    Re: valgrind leak I can't find Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-22 17:50 +0000
    Re: valgrind leak I can't find Bart <bc@freeuk.com> - 2024-08-23 12:18 +0100
      Re: valgrind leak I can't find Michael S <already5chosen@yahoo.com> - 2024-08-23 16:26 +0300
        Re: valgrind leak I can't find James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-23 09:39 -0400
          Re: valgrind leak I can't find James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-23 10:58 -0400
        Re: valgrind leak I can't find scott@slp53.sl.home (Scott Lurndal) - 2024-08-23 13:40 +0000
        Re: valgrind leak I can't find David Brown <david.brown@hesbynett.no> - 2024-08-23 15:41 +0200
    Re: valgrind leak I can't find James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-23 09:39 -0400

csiph-web