何故私はこのメモリが漏れているのかわかりません。私はすべての動的に作成されたメモリをリリースしていますが、まだ406個のリークがあります。どんなヒントも素晴らしいだろう。私はそれを把握しようと1週間を過ごしたとcrtdbg
(任意の行を表示しない)とVLD
とまだ運が使用されていません。ロングコードのため申し訳ありません:リンクリストとハッシュ配列を使用しているときにメモリリークが発生する
---------- Block 742 at 0x00F06D50: 56 bytes ----------
Call Stack:
c:\users\main\desktop\lab3123.c (113): lab3.exe!createNode + 0xA bytes
c:\users\main\desktop\lab3123.c (152): lab3.exe!addToArr + 0x9 bytes
c:\users\main\desktop\lab3123.c (66): lab3.exe!main + 0x10 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (555): lab3.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): lab3.exe!mainCRTStartup
0x76713677 (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x775B9F42 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x775B9F15 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
74 65 63 68 6E 6F 6C 6F 67 79 00 CD CD CD CD CD technolo gy......
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD 01 00 00 00 ........ ........
---------- Block 746 at 0x00F06E20: 56 bytes ----------
Call Stack:
c:\users\main\desktop\lab3123.c (113): lab3.exe!createNode + 0xA bytes
c:\users\main\desktop\lab3123.c (152): lab3.exe!addToArr + 0x9 bytes
c:\users\main\desktop\lab3123.c (66): lab3.exe!main + 0x10 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (555): lab3.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): lab3.exe!mainCRTStartup
0x76713677 (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x775B9F42 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x775B9F15 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
68 75 6D 61 6E 69 74 79 00 CD CD CD CD CD CD CD humanity ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD 01 00 00 00 ........ ........
Visual Leak Detector detected 406 memory leaks (26480 bytes).
Largest number used: 43684 bytes.
Total allocations: 57944 bytes.
Visual Leak Detector is now exiting.
Press any key to continue . . .
WORDNEW* createNode(char *str)
{
WORDNEW* w;
if(!(w = (WORDNEW*)malloc(sizeof(WORDNEW))))
printf("Memory Allocation Error"),
exit(100);
strcpy(w->str, str);
w->count = 1;
return w;
}
//addToArr: adds a word to the hash array or linked list if there is a collision
void addToArr(char *str, HASH_ARR_ELEM hashArr[]){
int homeAddress = 0;
int addResult = 0;
int probe = 0;
HASH_ARR_ELEM *ph;
WORDNEW *w;
WORDNEW *rWord;
rWord = NULL;
homeAddress = hashFunct(str);
ph = &hashArr[homeAddress];
if(ph->wordPtr == NULL){
if(!(ph->wordPtr = (WORDNEW*)malloc(sizeof(WORDNEW))))
printf("Memory Allocation Error"),
exit(100);
strcpy(ph->wordPtr->str, str);
ph->wordPtr->count = 1;
}else if(ph->wordPtr != NULL && ph->headPtr == NULL){
if(!(strcmp(ph->wordPtr->str, str)))
ph->wordPtr->count++;
else {
ph->headPtr = createList(cmpWord);
w = createNode(str);
addNode(ph->headPtr,w,&probe);
}
}else {
w = createNode(str);
if(!(strcmp(ph->wordPtr->str, str))){
ph->wordPtr->count++;
free(w);
}else if(retrieveNode(ph->headPtr,w,&rWord,&probe) == 1){
rWord->count++;
free(w);
}else
addNode(ph->headPtr,w,&probe);
} //end else
} // end addToArr
がヒントを受け取ります。必要に応じて、メモリリークのないようにプログラムを小さくして、空のメインに移動してください。その後徐々にあなたが漏れてはいけないと思うものを追加してください。漏れの原因を観察してください。 –
は(WORDNEW *)malloc(sizeof(WORDNEW))が解放されましたか? –
別のヒントは、少ないコードを投稿することです。どのように多くの人がSOの質問からすべてのコードをコンパイルするのか分かりませんが、代わりにそれを見直そうとします。より短いコードは、はるかに簡単で迅速にレビューすることができます。 –