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