问题
I'm compiling the code with -g flag and -O0. Please see valgrind output below.
I only want to know if I can I see what is invalid freed? I only see this:
==2566== Invalid free() / delete / delete[] / realloc()
==2566== at 0x4A21244: free (vg_replace_malloc.c:468)
==2566== by 0x500FB7A: free_mem (in /lib64/libc-2.4.so)
==2566== by 0x500F781: __libc_freeres (in /lib64/libc-2.4.so)
==2566== by 0x491C719: _vgnU_freeres (vg_preloaded.c:62)
==2566== by 0x4F4E6F4: exit (in /lib64/libc-2.4.so)
==2566== by 0x4F3930A: (below main) (in /lib64/libc-2.4.so)
==2566== Address 0x403ef10 is not stack'd, malloc'd or (recently) free'd
==2566==
==2566==
==2566== HEAP SUMMARY:
==2566== in use at exit: 2,416,506 bytes in 1,844 blocks
==2566== total heap usage: 62,683 allocs, 60,840 frees, 2,957,293 bytes allocated
==2566==
==2566== Searching for pointers to 1,844 not-freed blocks
==2566== Checked 5,445,056 bytes
The code:
int HoleInstrumentenDiffListeDB(GTree *tree)
{
OCI_Connection* cn;
OCI_Statement* st;
OCI_Resultset* rs;
if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
return EXIT_FAILURE;
char query[500];
query[0] ='\0';
cn = OCI_ConnectionCreate( "db", "u", "p", OCI_SESSION_DEFAULT);
st = OCI_StatementCreate(cn);
strcat(query, "SELECT ...");
OCI_ExecuteStmt(st, query);
rs = OCI_GetResultset(st);
int i = 1;
int j = 0;
char *symbolp;
while (OCI_FetchNext(rs)){
const char * symbolp = OCI_GetString(rs,2);
switch ( * OCI_GetString(rs,3))
{
case 'N':
insertQot(tree, symbolp, OCI_GetInt(rs, 1) );
printf("new \n");
break;
case 'U':
insertQot(tree, symbolp, OCI_GetInt(rs, 1) );
printf("upd \n");
break;
case 'D':
deleteQot(tree, symbolp);
printf("del \n");
break;
}
}
OCI_Cleanup();
return 1;
}
int main ()
{
GTree* t = g_tree_new_full((GCompareDataFunc)g_ascii_strcasecmp,NULL,g_free,g_free);
HoleInstrumentenListe(t);
HoleInstrumentenDiffListeDB(t);
g_tree_destroy (t);
}
回答1:
I think you should be freeing line
inside the while ()
loop, not at the end of the for ()
loop. I believe getline()
will only allocate memory if the first parameter is *NULL, so you are in effect using the buffer allocated by the very first call over and over again.
But it's been a long while since I wrote much C!
来源:https://stackoverflow.com/questions/22099570/invalid-free-delete-delete-realloc-in-libc-freeres