[英]Why is a destructor directly after calling an implicit type conversion constructor?
Give the following function: 提供以下功能:
osal_allocator* SharedMemoryManager::allocator();I
Where osal_allocator
is a 'c' structure, containing function pointers. 其中
osal_allocator
是一个'c'结构,包含函数指针。
And the a wrapper class that provides the following constructor: 以及提供以下构造函数的包装类:
Allocator::Allocator( osal_allocator* );
A function makes the following call: 一个函数进行以下调用:
001 SomeFunc( SharedMemoryManager* shm )
002 {
003 Allocator myAllocator = shm.allocator();
004
005 myAllocator.doSomething();
006
007 // stuff
008 }
The code fails with a SIG SEGV
. 代码因
SIG SEGV
而失败。 The reason is that on line 003
the destructor for myAllocator
is called immediately after its constructor is called. 原因是在第
003
行, myAllocator
的析构函数在调用其构造函数后立即被调用。 This means that myAllocator
is invalid on line 005
, since it has been destroyed. 这意味着
myAllocator
在第005
行无效,因为它已被销毁。
(Note: the default constructor is not being called and neither are any assignment operators). (注意:默认构造函数未被调用,也没有任何赋值运算符)。
If line 003
is changed to: 如果第
003
行更改为:
003 Allocator myAllocator( shm.allocator );
The function works as expected, with myAllocators
's destructor not being called until it goes out of scope. 该函数按预期工作,
myAllocators
的析构函数在超出范围之前不会被调用。
Unfortunately I have not been able to reproduce this issue with a simple example. 不幸的是,我用一个简单的例子无法重现这个问题。
I am using : 我在用 :
g++ (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3)
With the following options: 使用以下选项:
c++ -MD -D__LINUX__ -g -ansi -Wall -Wextra -Wformat -Wno-format-security -Woverloaded-virtual -Iinc
Why is the compiler generating a destructor call for the first example 为什么编译器会为第一个示例生成析构函数调用
Allocator myAllocator = shm.allocator();
is copy initialization and involves the call of a conversion constructor (from a osal_allocator*
) and a copy constructor (from the temporary Allocator
). 是复制初始化,涉及转换构造函数(来自
osal_allocator*
)和复制构造函数(来自临时Allocator
)的调用。 The destructor that's being called is that of the temporary Allocator
object. 被调用的析构函数是临时
Allocator
对象的析构函数。
The crash is probably due to a missing copy constructor, or poorly implemented copy constructor in Allocator
. 崩溃可能是由于缺少了复制构造函数,或者是
Allocator
实现不当的复制构造函数。
This is backed up by your claim that changing the initialization to 这可以通过将初始化更改为的声明来支持
Allocator myAllocator( shm.allocator );
works - this is because there's no copy involved - the conversion constructor is called directly, no temporary object is created. 工作 - 这是因为没有涉及复制 - 直接调用转换构造函数,不创建临时对象。
Read up on "the rule of three". 阅读“三规则”。
Basically, if a class requires either of a destructor, copy constructor or copy assignment operator (which is usually the case when a class manages resources), it requires all three of them . 基本上,如果一个类需要析构函数,复制构造函数或复制赋值运算符(当类管理资源时通常就是这种情况),它需要所有这三个 。
With this line 有了这条线
Allocator myAllocator = shm.allocator();
You are doing the following operations: 您正在执行以下操作:
Allocator
temporary object Allocator
临时对象 Allocator
where the rhs is the temporary Allocator
的拷贝构造函数,其中rhs是临时的 There are two possible operations which you din't consider and that may cause the SIG SEV: copy constructor and destructor. 您可能会考虑两种可能的操作,这可能会导致SIG SEV:复制构造函数和析构函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.