无效的 free() / delete / delete[] / realloc(),分配大小为 16 的块内的 4 个字节

Invalid free() / delete / delete[] / realloc(), 4 bytes inside a block of size 16 alloc'd

我用 C++ 编写了一个程序。它运行良好,直到最后,它吐出:* Error in `./xwd': free(): invalid pointer: 0x00000000017fd774 *

我的第一个想法是我在析构函数中做错了什么,但是由于相关的 class 没有析构函数,因为它没有使用 "new",这让我有点困惑.这让我第一次使用 valgrind,但这并没有带来太大的启发。

Valgrind 提出以下投诉:

2 errors in context 1 of 1:
==14245== Invalid free() / delete / delete[] / realloc()
==14245==    at 0x4C2D2E0: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14245==    by 0x407335: __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long) (new_allocator.h:110)
==14245==    by 0x406429: __gnu_cxx::__alloc_traits<std::allocator<int> >::deallocate(std::allocator<int>&, int*, unsigned long) (alloc_traits.h:185)
==14245==    by 0x404C07: std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long) (stl_vector.h:178)
==14245==    by 0x404796: std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&) (vector.tcc:394)
==14245==    by 0x402C21: std::vector<int, std::allocator<int> >::push_back(int const&) (stl_vector.h:925)
==14245==    by 0x40186C: Lexicon::Lexicon(char const*, unsigned int) (Lexicon.cpp:36)
==14245==    by 0x40B58A: main (xwd.cpp:20)
==14245==  Address 0x5a65114 is 4 bytes inside a block of size 16 alloc'd
==14245==    at 0x4C2C100: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14245==    by 0x40809D: __gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*) (new_allocator.h:104)
==14245==    by 0x4072B3: __gnu_cxx::__alloc_traits<std::allocator<int> >::allocate(std::allocator<int>&, unsigned long) (alloc_traits.h:182)
==14245==    by 0x4061CF: std::_Vector_base<int, std::allocator<int> >::_M_allocate(unsigned long) (stl_vector.h:170)
==14245==    by 0x404684: std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&) (vector.tcc:353)
==14245==    by 0x402C21: std::vector<int, std::allocator<int> >::push_back(int const&) (stl_vector.h:925)
==14245==    by 0x40186C: Lexicon::Lexicon(char const*, unsigned int) (Lexicon.cpp:36)
==14245==    by 0x40B58A: main (xwd.cpp:20)
==14245== 
==14245== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 0 from 0)

这指向 Lexicon.cpp (http://pastebin.com/Me4AcwXx) ,第 36 行,这是以下代码示例的倒数第二行:

for(unsigned int letter = 0; letter < next.length(); letter++){    
    assert(0<=pattern.length()&&pattern.length()<=index_vectors.size());
    assert(0<=letter && letter<=index_vectors[pattern.length()].size());
    assert(0<=pattern[letter]-'A' && pattern[letter]-'A'<=index_vectors[pattern.length()][letter].size());
  index_vectors[next.length()][letter][next[letter]-'A'].push_back(word_list_index);
  full_letter_vectors[next.length()].push_back(word_list_index);   }

在这里,word_list_index 只是一个整数。这张照片有什么问题?我在做令人发指的事情吗?在我看来这一切都很正常。

解决方法: 感谢所有建议我输入断言的人(上帝,研究生院把我变成了一个糟糕的程序员),这让我发现了问题:字典中的一个词是 "MP3",它不完全是字母数字.我没有意识到那种错误会等到解构时才弹出;我认为这会导致段错误。哎呀。

谢谢大家!