我正在使用-g标志和-O0编译代码。请参阅下面的valgrind输出。
我只想知道我是否可以看到什么是无效的?我只看到这个:
==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
代码:
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);
}
答案 0 :(得分:0)
我认为你应该在line
循环中释放while ()
,而不是在for ()
循环的末尾。我相信getline()
只会在第一个参数为* NULL时分配内存,所以你实际上是使用第一次调用分配的缓冲区一遍又一遍。
但是自从我写了很多C以来已经很久了!