为什么销毁被调用两次?

Why the destrcution is called twice?

本文关键字:两次 调用 为什么      更新时间:2023-10-16

我有一个崩溃。使用gdb对栈进行分析,得到如下结果:

13 0x00007f423c6e9670 in ?? ()
#14 0x00007f42340496d8 in ?? ()
#15 0x0000000003cef568 in ?? ()
#16 0x00000000008da861 in HuffmanEnd ()
#17 0x00000000008d4a83 in faacEncClose ()
#18 0x00000000004fd797 in RecorderSession::~RecorderSession   (this=0x7f423404ea90, __in_chrg=<value optimized out>)
at /root/Desktop/VideoRecoder/2.0/src/videorecorder/RecorderSession.cpp:203
#19 0x00000000004fdae9 in RecorderSession::~RecorderSession (this=0x7f423404ea90, __in_chrg=<value optimized out>)
at /root/Desktop/VideoRecoder/2.0/src/videorecorder/RecorderSession.cpp:203
#20 0x0000000000500d0b in RecorderSession::OnHangup (this=0x7f423404ea90) at /root/Desktop/VideoRecoder/2.0/src/videorecorder/RecorderSession.cpp:295
#21 0x000000000045e083 in CSipPhone::on_call_state (call_id=2, e=<value optimized out>)

正如我们所看到的,崩溃发生在HuffmanEnd。但是我不明白为什么~RecorderSession被调用两次,虽然我使用代码"删除这个"来删除RecorderSession对象如下:int RecorderSession: OnHangup (){删除;返回0;}

那么是"delete this"导致了这种现象吗?

很可能您的函数OnHangup本身已经从相关对象的析构函数中被调用了。因此,当对象已经处于销毁过程中时,您正在自己调用delete,从而导致双重删除。

似乎您的对象是通过放置新创建的,或作为堆栈上的本地对象,或作为命名空间范围/全局对象,或作为另一个对象的成员。

在这种情况下,医生将被调用一次。