gdb不知为何显示2次析构

本文探讨了使用GDB调试器时遇到的一个现象:虚析构函数被显示调用了两次。通过提供的代码示例和详细的GDB调试过程,分析了问题的原因,并指出这通常发生在使用delete操作符删除虚基类派生的对象时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

gdb不知为何显示2次析构

(金庆的专栏 2016.11)

gdb 显示2次 A::~A():

(gdb) bt
#0  A::~A (this=0x602010, __in_chrg=<optimized out>) at main.cpp:10
#1  0x0000000000400a96 in A::~A (this=0x602010, __in_chrg=<optimized out>)
    at main.cpp:12
#2  0x00000000004009c0 in main () at main.cpp:18

代码如下:

class A
{
public:
    A() {}
    virtual ~A() 
    {
        cout << "~A()" << endl;
    }
};

int main()
{
    A* p = new A;
    delete p;
    return 0;
}

打断点显示:2 locations:

(gdb) b A::~A
Breakpoint 1 at 0x400a40: A::~A. (2 locations)

完整的 gdb 输出:

g++ -g main.cpp
[jinq@localhost test]$ gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-80.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/jinq/test/a.out...done.
(gdb) b A::~A
Breakpoint 1 at 0x400a40: A::~A. (2 locations)
(gdb) run
Starting program: /home/jinq/test/a.out

Breakpoint 1, A::~A (this=0x602010, __in_chrg=<optimized out>) at main.cpp:12
12          }
Missing separate debuginfos, use: debuginfo-install glibc-2.17-106.el7_2.8.x86_64 libgcc-4.8.5-4.el7.x86_64 libstdc++-4.8.5-4.el7.x86_64
(gdb) bt
#0  A::~A (this=0x602010, __in_chrg=<optimized out>) at main.cpp:12
#1  0x00000000004009c0 in main () at main.cpp:18
(gdb) s

Breakpoint 1, A::~A (this=0x602010, __in_chrg=<optimized out>) at main.cpp:10
10          {
(gdb) bt
#0  A::~A (this=0x602010, __in_chrg=<optimized out>) at main.cpp:10
#1  0x0000000000400a96 in A::~A (this=0x602010, __in_chrg=<optimized out>)
    at main.cpp:12
#2  0x00000000004009c0 in main () at main.cpp:18
(gdb) ^CQuit
(gdb)

虚析构函数并且是delete才这样。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值