私はこれらの構造体を持っています:
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)
そして、私がこれらのエラーを修正するのを手伝ってください、ありがとう。