Hi all,
I've encountered a situation when destructors to local objects
are not called after an exception is raised. I use Borland C++
version 5.6.4 (Borland C++Builder 6). The examples are compiled
with following command to ensure that destructor cleanup is on:
bcc32 -RT -x -xd -I boosttest.cpp
bcc32 -RT -x -xd -I boosttest1.cpp
The code is attached.
Output of boosttest.cpp is:
C:\buf\temp\ttt>boosttest1
A is created! Global shared_ptr<A> destroyed!
A is created! Local A variable destroyed!
A is created! auto_ptr<A> destroyed!
A is created! scoped_ptr<A> destroyed!
A is created! Local shared_ptr<A> destroyed!
Access to global object through the local one! Global
shared_ptr<A> destroyed!
B is being created!
scoped_ptr<A> destroyed!
auto_ptr<A> destroyed!
Local A variable destroyed!
Exception cought: Too bad!
Global shared_ptr<A> destroyed!
Output of boosttest1.cpp is:
C:\buf\temp\ttt>boosttest1
A is created! Global shared_ptr<A> destroyed!
A is created! Local A variable destroyed!
A is created! auto_ptr<A> destroyed!
A is created! scoped_ptr<A> destroyed!
A is created! Local shared_ptr<A> destroyed!
Access to global object through the local one! Global
shared_ptr<A> destroyed!
B is being created!
scoped_ptr<A> destroyed!
auto_ptr<A> destroyed!
Local A variable destroyed!
Exception cought: Too bad!
Global shared_ptr<A> destroyed!
The problem is that the object created with the line
"shared_ptr<A> spa(new A("Local shared_ptr<A> destroyed!"));"
is destroyed in boosttest.cpp and is not destroyed in
boosttest1.cpp. If shared_ptr is replaced with plain pointer,
everything works fine (see boosttest2.cpp).
I would appreciate explaining what's wrong in my code.
TIA.