C++ 不使用虚析构的后果及分析
很多 C++ 方面的书籍都说明了虚析构的作用:
- 保证派生类的析构函数被调用,并且使析构顺序与构造函数相反
- 保证资源能够被正确释放
很久一段时间以来,我一直认为第 2 点仅仅指的是:当派生类使用 RAII 手法时,如果派生类的析构没有被调用,就会产生资源泄露。就像下面的代码:
#include <iostream>
struct A
{
A() {
std::cout << "A::A" << std::endl;
}
~A() {
std::cout << "A::~A" << std::endl;
}
};
struct B : A
{
B() {
x = new int;
std::cout << "B::B" << std::endl;
}
~B() {
delete x;
std::cout << "B::~B" << std::endl;
}
int* x;
};
int main()
{
A* a = new B;
delete a;
}
这段代码结果输出:
A::A
B::B
A::~A
B 的析构函数没被调用,a->x 没有被正确释放,产生了内存泄漏。
后来发现在多重继承情况下,情况可能更加严重。例如以下代码:
#include <iostream>
struct A1
{
A1() : a1(0) {}
~A1() {
std::cout << "A1::~A1" << std::endl;
}
int a1;
};
struct A2
{
A2() : a2(0) {}
~A2() {
std::cout << "A2::~A2" << std::endl;
}
int a2;
};
struct B : A1, A2
{
B() : b(0) {}
~B() {
std::cout << "B::~B" << std::endl;
}
int b;
};
int main()
{
B* b = new B;
A1* a1 = b;
A2* a2 = b;
printf("%p %p %p
", b, a1, a2);
delete a2;
}
输出:
0x5cbeb0 0x5cbeb0 0x5cbeb4
A2::~A2
free(): invalid pointer
已放弃 (核心已转储)
B* 隐式转型成 A2*,C++ 派生类指针(引用)转型为基类指针(引用)被称为 upcast。upcast 在单一继承的情况下,指针没有进行偏移,但是在多重继承下,会进行指针偏移。可以看到在多重继承下,第 2 个基类指针与派生类指针不同。再看 delete b
生成的汇编代码:
movq -40(%rbp), %rbx # %rbx = a2
testq %rbx, %rbx # a2 == 0 ?
je .L8
movq %rbx, %rdi # A2"s this ptr = a2
call A2::~A2() [complete object destructor]
movl $4, %esi
movq %rbx, %rdi
call operator delete(void*, unsigned long) # call operator delete(a2, 4)
可以看到先调用了 A2::~A2()
,再调用了 operator delete(a2, 12)
。 传给底层 free()
函数的指针是 a2(0x5cbeb4)
,正确的指针应该是 b(0x5cbeb0)
。而且第2个参数传递的是 4,是 A2 的大小,不是 B 的大小。free()
检测到这个是非法的指针,直接终止进程。给 A1
和 A2
的析构函数都加上 virtual
,执行结果为:
0x1eb2eb0 0x1eb2eb0 0x1eb2ec0
B::~B
A2::~A2
A1::~A1
执行结果是正常的,再看此时生成的汇编代码:
movq -40(%rbp), %rax # %rax = a2
testq %rax, %rax # a2 == 0 ?
je .L13
movq (%rax), %rdx # %rdx = vptr
addq $8, %rdx # %rdx = vptr + 8
movq (%rdx), %rdx # %rdx = vptr[1] or %rdx = *(vptr + 8)
movq %rax, %rdi # %rax = vptr[1]
call *%rdx # call vptr[1]
这段代码使用了虚函数,找到 B 的虚表:
vtable for B:
.quad 0
.quad typeinfo for B
.quad B::~B() [complete object destructor] # vptr B inherit A1
.quad B::~B() [deleting destructor]
.quad -16
.quad typeinfo for B
.quad non-virtual thunk to B::~B() [complete object destructor] # vptr B inherit A2
.quad non-virtual thunk to B::~B() [deleting destructor]
a2 的虚指针指向 non-virtual thunk to B::~B() [complete object destructor]
,会执行这个代码段:
non-virtual thunk to B::~B() [deleting destructor]:
subq $16, %rdi # this = a2 - 16 or this = b, a2 downcast to b
jmp .LTHUNK1
由于 a2 != b
,a2 要进行 downcast 变成 b,于是使用 thunk 技术进行指针偏移,再调用B::~B() [deleting destructor]
。B::~B() [deleting destructor]
再调用 B::~B(b)
,和 operator delete(b, 32)
.set .LTHUNK1,B::~B() [deleting destructor]
B::~B() [deleting destructor]:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movq %rdi, -8(%rbp) # store this to stack
movq -8(%rbp), %rax # %rax = this
movq %rax, %rdi
call B::~B() [complete object destructor] # call B::~B(b)
movq -8(%rbp), %rax
movl $32, %esi
movq %rax, %rdi
call operator delete(void*, unsigned long) # call operator delete(b, 32)
leave
ret
可以看到传递给 operator delete
的指针和大小是正确的。A2::~A2()
和 A1::~A1()
在 B::~B() [complete object destructor]
中被调用,不需要继续深入观察。
虚析构完美解决了这两个问题:
- 派生类的析构函数没有被调用
- 传递给底层
free()
函数的指针是错误的
在 ISO/IEC 14882:2011 5.3.3 也有对不使用虚析构的描述
In the first alternative (delete object), if the static type of the object to be deleted is different from its
dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the
static type shall have a virtual destructor or the behavior is undefined. In the second alternative (delete
array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.