1

I really need some help with memory leaks. I compiled code.c (shown bellow) with gcc -Wall -pedantic -Wno-long-long -g -std=c99 -fsanitize=address code.c and run it, Address Sanitizer not detect memory leaks.

How to detect these memory leaks, please?

Here is my code (code.c):

#include <stdio.h>
#include <stdlib.h>

#define VAL_LEN 50

typedef struct SItem {
    //int val;
    char val[VAL_LEN];
} TSITEM;

typedef struct S {
    TSITEM *m_Items;

    size_t m_Allocated;
    size_t m_Count;
} TS;

TS *S_create() {
    TS *newS = (TS *) malloc(sizeof (TS));

    newS->m_Allocated = 50;
    newS->m_Count = 0;
    newS->m_Items = (TSITEM*) malloc(newS->m_Allocated * sizeof (*newS->m_Items));

    return newS;
}

void S_destroy(TS *s) {
    //free(s->m_Items);  ------>  commented intentionally: this may occur memory leak, BUT NOT DETECTED BY -fsanitize=address

    s->m_Items = NULL;
    s->m_Count = 0;
    s->m_Allocated = 0;

    free(s);
}

int main(int argc, char** argv, char** envp) {
    TS *s = S_create();


    S_destroy(s);

    return (EXIT_SUCCESS);
}

UPDATE: Netbeans compiles my code with gcc -pedantic -Wno-long-long -fsanitize=address -c -g -Wall -std=c99 -MMD -MP -MF "build/Debug/GNU-Linux/tset.o.d" -o build/Debug/GNU-Linux/codde.o code.c

  • On a totally unrelated note, in C you [don't need to (and reallu shouldn't) cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Feb 08 '21 at 18:11
  • As for your problem of detecting memory leaks, perhaps use something like [Valgrind](https://valgrind.org/) instead? It's a good idea to use multiple tools. – Some programmer dude Feb 08 '21 at 18:12
  • I tried valgrind - used ´valgrind --leak-check=yes myprog arg1 arg2´ (from https://www.valgrind.org/docs/manual/quick-start.html#quick-start.mcrun) and this has no effect. Valgrind prints this: ´´´bash ==8668== All heap blocks were freed -- no leaks are possible ´´´ – Lukáš Jílek Feb 08 '21 at 18:26
  • Then it's time to look at the generated code. With the code you show the compiler might "optimize" away the function calls. Try doing something using the structures between the create and destroy calls. – Some programmer dude Feb 08 '21 at 18:36
  • 1
    The same code compiled with the same gcc toolchain gives me a leak: _SUMMARY: AddressSanitizer: 2500 byte(s) leaked in 1 allocation(s)._ What OS are you using? – David Ranieri Feb 08 '21 at 18:37
  • OT: `m_Items`, `m_Allocated`, .... we don't need all those `m_` in C, since we can not ommit the member's parent, there is no room for ambiguities. – David Ranieri Feb 08 '21 at 18:50
  • $ gcc --version gcc (Debian 8.3.0-6) 8.3.0 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. I use: $ uname -a Linux debian 4.19.0-14-amd64 #1 SMP Debian 4.19.171-2 (2021-01-30) x86_64 GNU/Linux – Lukáš Jílek Feb 08 '21 at 18:57
  • I use IDE NetBeans – Lukáš Jílek Feb 08 '21 at 19:01
  • Netbeans compiles my code with `gcc -pedantic -Wno-long-long -fsanitize=address -c -g -Wall -std=c99 -MMD -MP -MF "build/Debug/GNU-Linux/tset.o.d" -o build/Debug/GNU-Linux/codde.o code.c` – Lukáš Jílek Feb 08 '21 at 19:13
  • Omitting irrelevant flags, [it should report the leak](https://godbolt.org/z/jETcfj). – Some programmer dude Feb 08 '21 at 21:13

0 Answers0