Bug 20837 - Destructor appears twice in the stack trace
Summary: Destructor appears twice in the stack trace
Status: RESOLVED NOTABUG
Alias: None
Product: gdb
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.6
: P2 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-11-18 08:01 UTC by Jin Qing
Modified: 2022-12-23 19:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
simple test destructor (146 bytes, text/plain)
2016-11-18 08:01 UTC, Jin Qing
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jin Qing 2016-11-18 08:01:02 UTC
Created attachment 9645 [details]
simple test destructor

A::~A() appears twice:

    (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

Should be once.

See the attached code:

    class A
    {
    public:
        A() {}
        virtual ~A() 
        {
            cout << "~A()" << endl;
        }
    };
    
    int main()
    {
        A* p = new A;
        delete p;
        return 0;
    }
    
Breakpoint A::~A has 2 locations:

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

The full gdb session:

    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)
    
Twice if it is virtual destructor and is called by delete.

Also see: http://lists.qt-project.org/pipermail/interest/2015-November/019691.html
Comment 1 Tom Tromey 2022-12-23 19:10:56 UTC
Hi.  This is not a gdb bug, but instead a detail of how C++
is implemented by your compiler.  This is expected in the
ABI used by Linux.  If you search for "in charge destructor"
you might find more information about why this is done.

I'm going to close this.