Give the following function:
osal_allocator* SharedMemoryManager::allocator();I
Where osal_allocator is a ‘c’ structure, containing function pointers.
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. The reason is that on line 003 the destructor for myAllocator is called immediately after its constructor is called. This means that myAllocator is invalid on line 005, since it has been destroyed.
(Note: the default constructor is not being called and neither are any assignment operators).
If line 003 is changed to:
003 Allocator myAllocator( shm.allocator );
The function works as expected, with myAllocators‘s destructor not being called until it goes out of scope.
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
With this line
You are doing the following operations:
Allocatortemporary objectAllocatorwhere the rhs is the temporaryThere are two possible operations which you din’t consider and that may cause the SIG SEV: copy constructor and destructor.