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

时间:2015-05-15 13:14:18

标签: c++ memory-management vector valgrind

我用C ++编写了一个程序。它运行正常,直到结束,它会呕吐: *`./xwd'错误:free():无效指针:0x00000000017fd774 *

我的第一个想法是我在析构函数中做错了,但由于相关类没有析构函数,因为它不使用“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只是一个int。这张照片出了什么问题?我做了一件令人发指的事吗?这对我来说都很正常。

1 个答案:

答案 0 :(得分:0)

解决方案: 感谢所有提出建议的人(上帝,毕业学校让我变成了一个可怕的程序员),这让我抓住了问题:字典中的一个词是“MP3”,它不完全是字母数字。我没有意识到这种错误会等到解构弹出;我认为它会导致段错误。 YIKES。

全部谢谢!