free() 错误(使用 valgrind 调试)?
我有这些结构:
typedef struct _Frag{
struct _Frag *next;
char *seq;
int x1;
int length;
}Frag;
typedef struct _Fragment{
int type;
Frag *frag_list;
}Fragment;
然后我创建了一个数组
Fragment *fragments=malloc(1,sizeof(Fragment)); // or more
fragments->frag_list=malloc(1,sizeof(Frag)); // or more
Frag *frag=malloc(10,sizeof(Frag));
frag->seq="test str\n";
...
frag->next=malloc(1,sizeof(Frag));
frag->next->seq="test str\n";
在程序结束时,我想释放内存,功能是:
static void free_frags(){
int i;
Fragment *fragment;
Frag *current,*next;
for(i=0;i<1;i++){
fragment=&snp_frags[i];
current=fragment->frag_list;
next=current->next;
while(next!=NULL){
free(current->seq);
//free(current->next);
free(current);
current=next;
next=current->next;
}
free(current->seq);
//free(current->next);
free(current);
//free(fragment->frag_list);
free(&snp_frags[i]);
}
free(snp_frags);
}
如果我使用 valgrind 来调试它,valgrind 会说:
=============================================
==3810== Invalid read of size 4
==3810== at 0x80490FD: free_snp (hap.c:16)
==3810== by 0x80493AF: main (hap.c:73)
==3810== Address 0x41b139c is 12 bytes inside a block of size 296 free'd
==3810== at 0x4023EBA: free (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3810== by 0x8049167: free_snp (hap.c:30)
==3810== by 0x80493AF: main (hap.c:73)
==3810==
==3810== Invalid free() / delete / delete[]
==3810== at 0x4023EBA: free (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3810== by 0x8049167: free_snp (hap.c:30)
==3810== by 0x80493AF: main (hap.c:73)
==3810== Address 0x41b1398 is 8 bytes inside a block of size 296 free'd
==3810== at 0x4023EBA: free (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3810== by 0x8049167: free_snp (hap.c:30)
==3810== by 0x80493AF: main (hap.c:73)
请帮助我修复这些错误,谢谢。
I have these structs:
typedef struct _Frag{
struct _Frag *next;
char *seq;
int x1;
int length;
}Frag;
typedef struct _Fragment{
int type;
Frag *frag_list;
}Fragment;
And then I created a array
Fragment *fragments=malloc(1,sizeof(Fragment)); // or more
fragments->frag_list=malloc(1,sizeof(Frag)); // or more
Frag *frag=malloc(10,sizeof(Frag));
frag->seq="test str\n";
...
frag->next=malloc(1,sizeof(Frag));
frag->next->seq="test str\n";
At the end of program, I want to free the memory, the function is:
static void free_frags(){
int i;
Fragment *fragment;
Frag *current,*next;
for(i=0;i<1;i++){
fragment=&snp_frags[i];
current=fragment->frag_list;
next=current->next;
while(next!=NULL){
free(current->seq);
//free(current->next);
free(current);
current=next;
next=current->next;
}
free(current->seq);
//free(current->next);
free(current);
//free(fragment->frag_list);
free(&snp_frags[i]);
}
free(snp_frags);
}
If I use valgrind to debug it, valgrind says that:
=============================================
==3810== Invalid read of size 4
==3810== at 0x80490FD: free_snp (hap.c:16)
==3810== by 0x80493AF: main (hap.c:73)
==3810== Address 0x41b139c is 12 bytes inside a block of size 296 free'd
==3810== at 0x4023EBA: free (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3810== by 0x8049167: free_snp (hap.c:30)
==3810== by 0x80493AF: main (hap.c:73)
==3810==
==3810== Invalid free() / delete / delete[]
==3810== at 0x4023EBA: free (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3810== by 0x8049167: free_snp (hap.c:30)
==3810== by 0x80493AF: main (hap.c:73)
==3810== Address 0x41b1398 is 8 bytes inside a block of size 296 free'd
==3810== at 0x4023EBA: free (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3810== by 0x8049167: free_snp (hap.c:30)
==3810== by 0x80493AF: main (hap.c:73)
And please help me to fix these errors, thanx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果
您还没有
malloc
'ed内存块 - 字符串是在静态存储中分配的 - 然后您尝试free()
该内存块。您只能使用malloc()
分配free()
块,否则您将面临未定义行为的风险。您可以只将指向静态分配字符串的指针放入
Frag::seq
字段中,而从不free()
它们,或者您可以malloc()
内存对于这些字符串并将字符串复制到 malloc 的块中。In
you haven't
malloc
'ed the memory block - the string is allocated in static storage - and later you try tofree()
that memory block. You can onlyfree()
blocks allocated withmalloc()
, otherwise you risk running into undefined behaviour.You could either only put pointers to statically allocated strings into
Frag::seq
fields and neverfree()
them or you couldmalloc()
memory for these strings and copy the strings intomalloc
'ed blocks.molloc()
而不是malloc()
。检查你的元音。malloc()
- 它只需要一个。strcpy()
或strncpy()
或memcpy()
,具体取决于您对整个*cpy 的宗教观点()
混乱,将一个字符串的内容复制到另一个字符串中。molloc()
instead ofmalloc()
. Check your vowels.malloc()
with the wrong number of arguments - it only takes one.strcpy()
orstrncpy()
ormemcpy()
, depending on your religious perspective on the whole*cpy()
mess, to copy the contents of one string into another.您似乎是在说,您正在释放此内存,作为程序执行的最后一件事。
何苦呢?为什么不直接退出呢?那么你的释放将会更加完美,而且更快。 这实际上是推荐的技术。
我很确定没有评论者会这样做能够举出一个操作系统不从终止的程序中释放内存资源的示例。如果没有这个关键的操作系统功能,^C、kill、任务管理器、程序错误、程序崩溃……每个异常终止都会泄漏内存。
You seemed to be saying that you are freeing this memory as the last thing the program does.
Why bother? Why not just exit? Then your deallocation will be perfect, and faster. It is in fact the recommended technique.
I'm pretty sure no commenter will be able to cite an example of an OS that does not free memory resources from programs that terminate. Without this critical OS feature, ^C, kill, task manager, program bugs, program crashes ... every abnormal termination would leak memory.
删除代码行“free(fragment)”。效果会很好。
remove the code line "free(fragment)". It will work well.