gpt4 book ai didi

c - __libc_freeres() 中的 free()/delete/delete[]/realloc() 无效

转载 作者:行者123 更新时间:2023-11-30 17:38:31 25 4
gpt4 key购买 nike

我正在使用 -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);
}

最佳答案

我认为您应该在 while () 循环内释放 line,而不是在 for () 循环末尾。我相信 getline() 仅在第一个参数为 *NULL 时才会分配内存,因此您实际上是在一遍又一遍地使用第一次调用分配的缓冲区。

但是我已经很久没有写太多C了!

关于c - __libc_freeres() 中的 free()/delete/delete[]/realloc() 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22099570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com