我在工作中使用的某個應用程序遇到了一個奇怪而煩人的問題。該應用程序是用C++編寫的,當應用程序終止時(主函數返回或調用exit
),它會崩潰並出現分段錯誤。分段錯誤似乎是由basic_string類析構函數中的雙釋放指針造成的。我無法添加源代碼,但我可以說該應用程序非常簡單,我不直接在我的代碼中使用任何指針。該應用程序只是從庫中調用一個函數。SegFault退出應用程序
Valgrid標識以下問題:
==5402== Invalid read of size 4
==5402== at 0x549F05F: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (atomicity.h:49)
==5402== by 0x41D1BA4: ??? (in ../Functions.so)
==5402== by 0x436D873: ??? (in ../Functions.so)
==5402== by 0x967674: _dl_fini (in /lib/ld-2.12.so)
==5402== by 0x9A7EAE: exit (in /lib/libc-2.12.so)
==5402== by 0x810F8C2: main (Checker.C:146)
==5402== Address 0x55ec808 is 8 bytes inside a block of size 15 free'd
==5402== at 0x4007895: operator delete(void*) (vg_replace_malloc.c:480)
==5402== by 0x549EF67: std::string::_Rep::_M_destroy(std::allocator<char> const&) (new_allocator.h:110)
==5402== by 0x810F8C2: main (Checker.C:146)
==5402==
==5402== Invalid free()/delete/delete[]/realloc()
==5402== at 0x4007895: operator delete(void*) (vg_replace_malloc.c:480)
==5402== by 0x549EF67: std::string::_Rep::_M_destroy(std::allocator<char> const&) (new_allocator.h:110)
==5402== by 0x41D1BA4: ??? (in ..../Functions.so)
==5402== by 0x436D873: ??? (in .../Functions.so)
==5402== by 0x967674: _dl_fini (in /lib/ld-2.12.so)
==5402== by 0x9A7EAE: exit (in /lib/libc-2.12.so)
==5402== by 0x810F8C2: main (Checker.C:146)
==5402== Address 0x55ec800 is 0 bytes inside a block of size 15 free'd
==5402== at 0x4007895: operator delete(void*) (vg_replace_malloc.c:480)
==5402== by 0x549EF67: std::string::_Rep::_M_destroy(std::allocator<char> const&) (new_allocator.h:110)
==5402== by 0x810F8C2: main (Checker.C:146)
==5402==
應用程序被鏈接到共享和靜態庫。 Function.so
是一個可能包含一些靜態代碼的共享庫。這個問題與鏈接階段有關,因爲根據庫鏈接到我的可執行文件的順序,應用程序可能不會崩潰。
我真的很努力地解決這個問題,任何想法可能是這個問題的根源呢?任何建議如何着手調查這個問題?
你可以看看圖書館的代碼裏面嗎?也許一些靜態數據具有錯誤的析構函數或構造函數。 –
是的,我擁有lib。我可以看看它 –