Несоответствие в сводной ошибке кучи

0

Я знаю, что мне не нужно беспокоиться о байтах Still достижимый, но этот случай отличается.

Мои файлы: wscramble_fio.cpp

// wscramble.cpp
// Word Scramble guessing game
// Illustrates string library functions, character arrays,
// arrays of pointers, etc.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <fstream>

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 << "Usage : " << argv[0] << "<wordbankFile>" << endl ;
    return -1 ;
  }

  ifstream inputFile ;
  inputFile.open(argv[1]);

  if (!inputFile.is_open())
  {
    cout << "Error opening file : " << argv[1] << endl ;
    return -1 ;
  }

  int wordCount = 0 ;

  inputFile >> wordCount ;
  if (inputFile.fail())
  {
    cout << "File format incorrect" << endl ;
    return -1 ;
  }

  // Ignore the \n following the number.
  //inputFile.ignore(1024, '\n') ;

  char ** wordBank = new char* [wordCount] ;
  char buffer[41] ;
  int i = 0 ;
  while (!inputFile.eof())
  {
    inputFile >> buffer ;
    //cout << buffer ;
    if (strcmp(buffer,"\n") == 0 || strcmp(buffer," ") == 0 || strcmp(buffer, "") == 0)
    {
      continue ;
    }

    if (i >= wordCount)
    {
      cout << "Too many words in the file" << endl ;
      inputFile.close() ;
      return -1 ;
    }
    wordBank[i] = new char [strlen(buffer)+1] ;
    strcpy(wordBank[i],buffer) ;
    buffer[0] = '\0' ;
    i++ ;
  }

  for (int i = 0 ; i < wordCount ;i++)
  {
    cout << wordBank[i] << endl ;
  }

  srand(time(0));
  char guess[80];

  bool wordGuessed = false;
  int numTurns = 10;

  // Pick a random word from the wordBank
  int target = rand() % wordCount;
  int targetLen = strlen(wordBank[target]);

  // More initialization code
  char* word = new char[targetLen+1];
  strcpy(word, wordBank[target]);
  permute(word, targetLen);

  // An individual game continues until a word
  // is guessed correctly or 10 turns have elapsed
  while ( !wordGuessed && numTurns > 0 ){
    cout << "\nCurrent word: " << word << endl;
    cin >> guess;
    wordGuessed = (strncmp(guess, wordBank[target], targetLen+1) == 0);
    numTurns--;
  }
  if(wordGuessed){
    cout << "You win!" << endl;
  }
  else {
    cout << "Too many turns...You lose!" << endl;
  }


  /* This would go at the end of program. For now i m just writing here */
  for (int i = 0 ; i < wordCount ; i++)
  delete (wordBank[i]) ;
  delete [] wordBank ;
  inputFile.close() ;
}

// Scramble the letters
void permute(char items[], int len)
{
  for(int i=len-1; i > 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

Когда я использую команду:

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

Команда valgrind приводит к следующему результату.

     ==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)
  • 0
    Хакерское решение состоит в том, чтобы найти все new s и убедиться, что каждый соответствует с delete . Подсказка: char* word = new char[targetLen+1]; , Правильным решением является использование RAII - замените указатели и динамические массивы на std::vector .
  • 0
    Valgrind говорит вам, что у вас есть 10 блоков (allocs (new)) и 0 free. Это означает, что вся выделенная вами память все еще остается в куче. Это все еще достижимо, потому что вы не потеряли ссылку ни на одного из них во время казни. попробуйте добавить delete(word) в конце и посмотрите, что произойдет. Затем вы можете найти все new предложения и объявить delete() для каждого.
Теги:
pointers

2 ответа

1

Вы должны повторно запустить свою программу под valgrind, используя опцию "--leak-check = full -show-reachable = yes", чтобы получить полную трассировку стека, в которой вы протекаете. Об этом говорит Вальгринд в приведенном выше докладе.

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

Если вы хотите присоединить отладчик, когда утечка обнаружена Valgrind, чтобы вы могли выполнять живую отладку, вы должны использовать следующую команду:

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

Таким образом, ваша программа будет автоматически подключаться GDB всякий раз, когда ошибка встречается valgrind.

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

Вы не выпускаете его

Там также ошибка в этом деле:

delete (wordBank[i]);

wordBank[i] - char* поэтому правильное утверждение должно быть следующим:

delete[] (wordBank[i]);
  • 0
    Я добавил delete [] word; после последнего использования переменного слова все равно ошибка остается.
  • 0
    использовать просто delete(word);
Показать ещё 2 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню