Mismatched in Heap summary error

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

I know I do not need to worry about the Still reachable bytes, but this case is different.

My files: wscramble_fio.cpp

// wscramble.cpp // Word Scramble guessing game // Illustrates string library functions, character arrays, // arrays of pointers, etc. #include  #include  #include  #include  #include  #include   using namespace std;  // Prototype void permute(char items[], int len);  // Define an array of strings (since a string is just a char array) // and since string are just char *'s, we really want an array of char *'s   int main(int argc, char * argv[]) {   if (argc != 2)   {     cout " > wordCount ;   if (inputFile.fail())   {     cout > buffer ;     //cout = wordCount)     {       cout  0 ){     cout > guess;     wordGuessed = (strncmp(guess, wordBank[target], targetLen+1) == 0);     numTurns--;   }   if(wordGuessed){     cout  0; --i){     int r = rand() % i;     int temp = items[i];     items[i] = items[r];     items[r] = temp;   }  } 

wordbank.txt

6  cs103 trojan midterm  aced  perfect  score 

When I use the command:

valgrind --tool=memcheck --leak-check=yes ./wscramble_fio wordbank.txt 

The valgrind command results in the following output.

     ==10409== Mismatched free() / delete / delete [] ==10409==    at 0x4C2BADC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==10409==    by 0x4015D7: main (in /home/simpleguy/CCPP/wscramble_fio) ==10409==  Address 0x5a1c550 is 0 bytes inside a block of size 5 alloc'd ==10409==    at 0x4C2AFE7: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==10409==    by 0x401489: main (in /home/simpleguy/CCPP/wscramble_fio) ==10409==  ==10409== Mismatched free() / delete / delete [] ==10409==    at 0x4C2BADC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==10409==    by 0x401608: main (in /home/simpleguy/CCPP/wscramble_fio) ==10409==  Address 0x5a1c370 is 0 bytes inside a block of size 6 alloc'd ==10409==    at 0x4C2AFE7: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==10409==    by 0x40135D: main (in /home/simpleguy/CCPP/wscramble_fio) ==10409==  ==10409==  ==10409== HEAP SUMMARY: ==10409==     in use at exit: 0 bytes in 0 blocks ==10409==   total heap usage: 10 allocs, 10 frees, 8,853 bytes allocated ==10409==  ==10409== All heap blocks were freed -- no leaks are possible ==10409==  ==10409== For counts of detected and suppressed errors, rerun with: -v ==10409== ERROR SUMMARY: 7 errors from 2 contexts (suppressed: 2 from 2) 

回答1:

You should rerun your program under valgrind using the option "--leak-check=full --show-reachable=yes" to get the complete stack trace where you are leaking. This is suggested by the Valgrind in the above report.

$ valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./wscramble_fio wordbank.txt

If you want to attach by the debugger whenever leak is detected by the Valgrind so that you can do live debugging, you should use the following command:

$ valgrind --tool=memcheck --leak-check=yes --db-attach=yes ./wscramble_fio wordbank.txt

By this way, your program would automatically be attached by GDB whenever error encounter by valgrind.



回答2:

char* word = new char[targetLen+1]; 

You are not releasing it

There's also an error in this deletion:

delete (wordBank[i]); 

wordBank[i] is a char* so the correct statement should be the following:

delete[] (wordBank[i]); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!