valgrind leak I can't find

Liste des GroupesRevenir à cl c 
Sujet : valgrind leak I can't find
De : mark (at) *nospam* qtrac.eu (Mark Summerfield)
Groupes : comp.lang.c
Date : 22. Aug 2024, 10:41:46
Autres entêtes
Message-ID : <j8idnQlHTPZXZFv7nZ2dnZfqn_GdnZ2d@brightview.co.uk>
User-Agent : Pan/0.154 (Izium; 517acf4)
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.

Date Sujet#  Auteur
22 Aug 24 * valgrind leak I can't find26Mark Summerfield
22 Aug 24 +- Re: valgrind leak I can't find1Ben Bacarisse
22 Aug 24 +* Re: valgrind leak I can't find11Bart
22 Aug 24 i`* Re: valgrind leak I can't find10Thiago Adams
22 Aug 24 i `* Re: valgrind leak I can't find9Annada Behera
22 Aug 24 i  +* Naming conventions (was Re: valgrind leak I can't find)6Janis Papanagnou
22 Aug 24 i  i`* Re: Naming conventions (was Re: valgrind leak I can't find)5Thiago Adams
23 Aug 24 i  i +- Re: Naming conventions (was Re: valgrind leak I can't find)1Janis Papanagnou
23 Aug 24 i  i `* Re: Naming conventions (was Re: valgrind leak I can't find)3James Kuyper
25 Aug 24 i  i  `* Re: Naming conventions (was Re: valgrind leak I can't find)2Janis Papanagnou
26 Aug 24 i  i   `- Re: Naming conventions (was Re: valgrind leak I can't find)1James Kuyper
24 Aug 24 i  `* Re: valgrind leak I can't find2Blue-Maned_Hawk
24 Aug 24 i   `- Re: valgrind leak I can't find1Keith Thompson
22 Aug 24 +- Re: valgrind leak I can't find1Stefan Ram
22 Aug 24 +- Re: valgrind leak I can't find1Ike Naar
22 Aug 24 +- Re: valgrind leak I can't find1Stefan Ram
22 Aug 24 +- Re: valgrind leak I can't find1Kaz Kylheku
22 Aug 24 `* Re: valgrind leak I can't find9Mark Summerfield
22 Aug 24  +- Re: valgrind leak I can't find1Kaz Kylheku
23 Aug 24  +* Re: valgrind leak I can't find6Stefan Ram
23 Aug 24  i`* Re: valgrind leak I can't find5Bart
23 Aug 24  i `* Re: valgrind leak I can't find4Michael S
23 Aug 24  i  +* Re: valgrind leak I can't find2James Kuyper
23 Aug 24  i  i`- Re: valgrind leak I can't find1James Kuyper
23 Aug 24  i  `- Re: valgrind leak I can't find1David Brown
23 Aug 24  `- Re: valgrind leak I can't find1James Kuyper

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal