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

时间:2021-05-06 04:26:24

I've written a program using C++. It runs fine until the end, where it vomits: * Error in `./xwd': free(): invalid pointer: 0x00000000017fd774 *

我用c++写了一个程序。它一直运行得很好,直到最后它呕吐:*错误。/xwd': free():无效指针:0x00000000017fd774 *

My first thought was that I was doing something wrong in the destructor, but as the relevant class has no destructor as it doesn't use "new", this was a little confusing to me. This led me to use valgrind for the first time, but that hasn't been terribly illuminating.

我的第一个想法是,我在析构函数中做了一些错误的事情,但是由于相关类没有析构函数,因为它不使用“new”,这让我有点困惑。这让我第一次使用了valgrind,但这并不是很有启发性。

Valgrind gives the following complaint:

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)

This points to Lexicon.cpp (http://pastebin.com/Me4AcwXx) ,line 36, which is the second-to-last line of the following code sample:

这指的是词汇。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);   }

Here, word_list_index is simply an int. What's wrong with this picture? Am I doing something heinous? It all looks pretty normal to me.

在这里,word_list_index只是一个int类型的值。我在做什么可憎的事吗?这一切在我看来都很正常。

1 个解决方案

#1


0  

Solution: Thanks to everyone who suggestion I put in asserts (god, grad school has turned me into a TERRIBLE programmer), which led me to catch the problem: one of the words in the dictionary was "MP3" which wasn't entirely alphanumeric. I didn't realize that that kind of error would wait until deconstruction to pop up; I assumed it would cause a segfault. YIKES.

解决方案:感谢每个人的建议,我在断言(上帝,研究生院把我变成了一个糟糕的程序员),这让我发现了问题:字典里的一个词是MP3,它不完全是字母数字。我没有意识到这种错误会等到解构主义出现;我以为这会导致赛格失误。呵。

Thanks all!

感谢所有!

#1


0  

Solution: Thanks to everyone who suggestion I put in asserts (god, grad school has turned me into a TERRIBLE programmer), which led me to catch the problem: one of the words in the dictionary was "MP3" which wasn't entirely alphanumeric. I didn't realize that that kind of error would wait until deconstruction to pop up; I assumed it would cause a segfault. YIKES.

解决方案:感谢每个人的建议,我在断言(上帝,研究生院把我变成了一个糟糕的程序员),这让我发现了问题:字典里的一个词是MP3,它不完全是字母数字。我没有意识到这种错误会等到解构主义出现;我以为这会导致赛格失误。呵。

Thanks all!

感谢所有!