You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GDB能否显示正在析构的变量?如何定位C++对象析构断言问题

析构函数触发断言时无法定位具体实例

当Foo类的析构函数触发assert断言时,GDB调试仅显示退出main()作用域,无法定位是哪个Foo实例(实际为b)导致的断言触发。期望调试时能像构造函数调用那样,在main()返回时依次跳转到Foo c;、Foo b;等对应的行,明确显示当前析构的对象。

示例代码

#include <assert.h>

int v = 0;

struct Foo {
    Foo() { ++v; }
    ~Foo()
    {
        --v;
        assert(v != 1); // artificially stop the debugger here
    }
};

int main()
{
    Foo a; // assert in destructor here?
    Foo b; // assert in destructor here?
    Foo c; // assert in destructor here?
    return 0;
} // main()

GDB回溯信息

#6  0x00007ffff7d9ee96 in __GI___assert_fail (assertion=0x555555556019 "v != 1", file=0x555555556010 "main.cpp", line=8, 
    function=0x555555556004 "Foo::~Foo()") at ./assert/assert.c:101
#7  0x000055555555521c in Foo::~Foo (this=<optimized out>, __in_chrg=<optimized out>) at main.cpp:8
#8  0x00005555555551b6 in main () at main.cpp:17
(gdb) f 8
#8  0x00005555555551b6 in main () at main.cpp:17
17      } // main()

疑问

  • 已知问题出在Foo b,但如何让GCC/GDB提供该信息?
  • 这是功能缺失还是存在底层限制?
  • 为每个实例添加作用域可定位问题,但不想重新编译。此外,类成员对象的析构也存在相同定位问题。

内容的提问来源于stack exchange,提问作者jozxyqk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 10:50:39