2015-05-15 25 views
0

我已經使用C++編寫了一個程序。它運行良好,直到結束,在那裏嘔吐:*錯誤的`./xwd':免費():無效的指針:0x00000000017fd774 *無效的空閒()/刪除/刪除[]/realloc(),一個大小爲16的塊內的4個字節alloc'd

我首先想到的是,我是做錯事的析構函數,但因爲相關的類沒有析構函數,因爲它不使用「新的」,這對我來說有點混亂。這導致我第一次使用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。這張照片有什麼問題?我在做一件令人髮指的事嗎?這一切對我來說都很正常。

+3

您需要發佈一個完整的重現示例。關於該代碼的唯一明顯跡象是一堆可能超出範圍的索引用法。 – Puppy

+0

Valgrind正在調用'Lexicon :: Lexicon(char const *,unsigned int)'。你能爲此顯示代碼嗎? – NathanOliver

+0

你確定你在'index_vectors [next.length()] [letter] [next [letter] - 'A']'沒有出界限制嗎? – Jarod42

回答

0

解決方案: 感謝大家對我提出的建議(上帝,畢業學校已經把我變成了一個可怕的程序員),這導致我發現了這個問題:字典中的一個單詞是「MP3」,它是並不完全是字母數字。我沒有意識到這種錯誤會等到解構出來纔會出現;我認爲這會導致段錯誤。 YIKES。

謝謝大家!