这个额外的参数传递给虚拟析构函数是什么? [英] What's this extra parameter passed into virtual destructor?
本文介绍了这个额外的参数传递给虚拟析构函数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码:
class Class $ {
public:
virtual〜 {}
};
int main()
{
Class * object = new Class();
delete object;
}
我使用Visual C ++ 10编译并得到这个反汇编 delete object
语句:
test eax,eax
je wmain + 23h(401041h)
mov edx,dword ptr [eax]
push 1
mov ecx,eax
call dword ptr [edx]
这是实际的析构函数:
Class ::`scalar deleting destructor':
test byte ptr [esp + 4],1
push esi
mov esi ,ecx
mov dword ptr [esi],offset Class ::`vftable'(402100h)
je Class ::`标量删除析构函数'+ 18h(401018h)
push esi
call dword ptr [__imp_operator delete(4020A8h)]
pop ecx
mov eax,esi
pop esi
ret 4
push 1
在调用站点做什么以及为什么是
在析构函数入口点检查该值并有条件地绕过调用 operator delete()
?
解决方案
参数被析构函数用来知道是否应该在结束时调用delete。
您不想调用它:
- 析构函数由派生类析构函数调用
- 对象是另一个对象的字段,因此不是由新创建的
编辑:添加第三个案例
I have this code:
class Class {
public:
virtual ~Class() {}
};
int main()
{
Class* object = new Class();
delete object;
}
which I compile with Visual C++ 10 and get this disassembly for delete object
statement:
delete object;
test eax,eax
je wmain+23h (401041h)
mov edx,dword ptr [eax]
push 1
mov ecx,eax
call dword ptr [edx]
and this for the actual destructor:
Class::`scalar deleting destructor':
test byte ptr [esp+4],1
push esi
mov esi,ecx
mov dword ptr [esi],offset Class::`vftable' (402100h)
je Class::`scalar deleting destructor'+18h (401018h)
push esi
call dword ptr [__imp_operator delete (4020A8h)]
pop ecx
mov eax,esi
pop esi
ret 4
What is that push 1
doing at the call site and why is the test
at the destructor entry point checking for that value and conditionally bypass call to operator delete()
?
解决方案
The argument is used by the destructor to know if it should call delete at the end.
3 cases where you don't want to call it :
- The destructor is called by a derived class destructor
- The object is allocated on the stack, thus not created with new.
- The object is a field of another object, thus not created by new
EDIT: Add a third case
这篇关于这个额外的参数传递给虚拟析构函数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文