应用程序退出时出现SegFault

SegFault on application exit

本文关键字:SegFault 退出 应用程序      更新时间:2023-10-16

我在工作中使用的一个应用程序遇到了一个奇怪而烦人的问题。应用程序是用C++编写的,当应用程序终止时(调用主函数return或exit(,它会因分段错误而崩溃。分段错误似乎是由basic_string类析构函数中的双释放指针引起的。我不能添加源代码,但我可以说这个应用程序非常简单,我没有直接在代码中使用任何指针。该应用程序只是在调用库中的一个函数。

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是一个共享库,它可能包含一些静态代码。这个问题与链接阶段有关,因为根据库链接到我的可执行文件的顺序,应用程序可能不会崩溃。

我真的很难解决这个问题,你知道这个问题的根本原因是什么吗?有什么建议可以继续调查这个问题吗?

这个问题的根本原因是代码中的某个错误。bug可以是任何东西。野生指针取消引用,从数组末尾运行,或任何数量的其他无数类型的错误。C++代码中的错误并不一定意味着应用程序会立即崩溃。应用程序可以继续执行,但在稍后尝试访问由错误导致的损坏内存时会崩溃。

代码中的某个地方有一个错误,可能会导致内存损坏,当应用程序终止时会触发此崩溃。您需要找到并修复它。欢迎使用C++。