Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Destructor is not always called after exception is thrown in the scope


This question is not answered. Helpful answers available: 0. Correct answers available: 1.


Permlink Replies: 10 - Last Post: Oct 15, 2015 7:17 AM Last Post By: david hoke
Ville-Valtteri ...

Posts: 13
Registered: 6/18/15
Destructor is not always called after exception is thrown in the scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2015 5:02 AM
Hello, we are experiencing a problem in which every instance in a scope is not destructed after an exception is thrown. This problem occurs only in Release build, and never in Debug build. What seems to make difference are debug symbols; for example a Debug build with all optimizations work fine.

We are using Embarcadero C++ Builder XE8 version 22.0.19027.8951, TwineCompile 4.2.0.47, and Boost 1.39.0. Target platform is 32-bit Windows and development platform is 64-bit Windows 7 sp 1. Language standard is C++03.

The following code shows the problem and our workaround:

Definition of ScopeGuard:
	struct ScopeGuard : boost::noncopyable{
	public:
		typedef boost::function<void(void)> function_t;
		ScopeGuard(function_t f) : f(f) {}
		~ScopeGuard() {
			try{
				f();
			} catch(...) {}
		}
 
	private:
		function_t f;
	};
 
Example 1:
 
	ScopeGuard sg(function);
	try{
		throw OurException();
	}
	catch(...){
		throw;
	} // destructor of sg is never called
 
Example 2:
 
	ScopeGuard sg(function);
	try{
		throw OurException();
	}
	catch(const OurException& e){
		throw;
	} // destructor of sg is always called
 
Example 3:
 
	boost::shared_ptr<ScopeGuard> sg(new ScopeGuard(function));
	try{
		throw OurException();
	}
	catch(...){
		throw;
	} // destructor of sg is always called


The problem exists at least with the ScopeGuard class. Under some circumstances, even destructors of boost::shared_ptr are not called, but I have not been able to create minimal working examples for that. Something similar to this triggers the problem:

struct Driver {
	boost::shared_ptr<OurException> exception;
} driver;
 
boost::shared_ptr<Attachment> attachment = createAttachment();
 
if (driver.exception) {
	throw *driver.exception;
}
// destructor of attachment is never called


Is this a known bug in the compiler, and are the symptoms documented somewhere? If this has been fixed, which version of the compiler works?

If this is not a bug in the compiler, how can we fix this?
david hoke

Posts: 616
Registered: 2/9/07
Re: Destructor is not always called after exception is thrown in the scope
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2015 6:30 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
I have no solutions for you, but some thoughts/questions, and some
possibly similar old history...
history:
http://www.borlandtalk.com/temporarily-missing-local-object-destructor-calls-bad-co-vt99850.html
http://qc.embarcadero.com/wc/qcmain.aspx?d=33712
http://www.delphigroups.info/3/8/117547.html

questions:
1)Are you using classic or clang?
2)If classic, have you tried the 32bit clang compiler (seems
problematic for some, but might try)
3)Have you tried 'release' build, but telling it to generate debugging
information?

I wonder if they never actually got rid of the switch controlling local
destructor generation internally, but maybe just took away command line
parameters accessing it...

Ville-Valtteri Tiittanen wrote:

Hello, we are experiencing a problem in which every instance in a
scope is not destructed after an exception is thrown. This problem
occurs only in Release build, and never in Debug build. What seems to
make difference are debug symbols; for example a Debug build with all
optimizations work fine.

We are using Embarcadero C++ Builder XE8 version 22.0.19027.8951,
TwineCompile 4.2.0.47, and Boost 1.39.0. Target platform is 32-bit
Windows and development platform is 64-bit Windows 7 sp 1. Language
standard is C++03.

The following code shows the problem and our workaround:

Definition of ScopeGuard:
	struct ScopeGuard : boost::noncopyable{
	public:
		typedef boost::function<void(void)> function_t;
		ScopeGuard(function_t f) : f(f) {}
		~ScopeGuard() {
			try{
				f();
			} catch(...) {}
		}
 
	private:
		function_t f;
	};
 
Example 1:
 
	ScopeGuard sg(function);
	try{
		throw OurException();
	}
	catch(...){
		throw;
	} // destructor of sg is never called
 
Example 2:
 
	ScopeGuard sg(function);
	try{
		throw OurException();
	}
	catch(const OurException& e){
		throw;
	} // destructor of sg is always called
 
Example 3:
 
	boost::shared_ptr<ScopeGuard> sg(new ScopeGuard(function));
	try{
		throw OurException();
	}
	catch(...){
		throw;
	} // destructor of sg is always called


The problem exists at least with the ScopeGuard class. Under some
circumstances, even destructors of boost::shared_ptr are not called,
but I have not been able to create minimal working examples for that.
Something similar to this triggers the problem:

struct Driver {
	boost::shared_ptr<OurException> exception;
} driver;
 
boost::shared_ptr<Attachment> attachment = createAttachment();
 
if (driver.exception) {
	throw *driver.exception;
}
// destructor of attachment is never called


Is this a known bug in the compiler, and are the symptoms documented
somewhere? If this has been fixed, which version of the compiler
works?

If this is not a bug in the compiler, how can we fix this?
david hoke

Posts: 616
Registered: 2/9/07
Re: Destructor is not always called after exception is thrown in the scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2015 6:43 AM   in response to: david hoke in response to: david hoke
Oops, also failed to ask:
Is it evident only with 'locally' defined objects, as I believe all of
your examples are?
That might come out if you look at the links, but should have
asked/highlighted in previous message...

david hoke wrote:

I have no solutions for you, but some thoughts/questions, and some
possibly similar old history...
history:
http://www.borlandtalk.com/temporarily-missing-local-object-destructor-calls-bad-co-vt99850.html
http://qc.embarcadero.com/wc/qcmain.aspx?d=33712
http://www.delphigroups.info/3/8/117547.html

questions:
1)Are you using classic or clang?
2)If classic, have you tried the 32bit clang compiler (seems
problematic for some, but might try)
3)Have you tried 'release' build, but telling it to generate debugging
information?

I wonder if they never actually got rid of the switch controlling
local destructor generation internally, but maybe just took away
command line parameters accessing it...

Ville-Valtteri Tiittanen wrote:

Hello, we are experiencing a problem in which every instance in a
scope is not destructed after an exception is thrown. This problem
occurs only in Release build, and never in Debug build. What seems
to make difference are debug symbols; for example a Debug build
with all optimizations work fine.

We are using Embarcadero C++ Builder XE8 version 22.0.19027.8951,
TwineCompile 4.2.0.47, and Boost 1.39.0. Target platform is 32-bit
Windows and development platform is 64-bit Windows 7 sp 1. Language
standard is C++03.

The following code shows the problem and our workaround:

Definition of ScopeGuard:
	struct ScopeGuard : boost::noncopyable{
	public:
		typedef boost::function<void(void)> function_t;
		ScopeGuard(function_t f) : f(f) {}
		~ScopeGuard() {
			try{
				f();
			} catch(...) {}
		}
 
	private:
		function_t f;
	};
 
Example 1:
 
	ScopeGuard sg(function);
	try{
		throw OurException();
	}
	catch(...){
		throw;
	} // destructor of sg is never called
 
Example 2:
 
	ScopeGuard sg(function);
	try{
		throw OurException();
	}
	catch(const OurException& e){
		throw;
	} // destructor of sg is always called
 
Example 3:
 
	boost::shared_ptr<ScopeGuard> sg(new ScopeGuard(function));
	try{
		throw OurException();
	}
	catch(...){
		throw;
	} // destructor of sg is always called


The problem exists at least with the ScopeGuard class. Under some
circumstances, even destructors of boost::shared_ptr are not called,
but I have not been able to create minimal working examples for
that. Something similar to this triggers the problem:

struct Driver {
	boost::shared_ptr<OurException> exception;
} driver;
 
boost::shared_ptr<Attachment> attachment = createAttachment();
 
if (driver.exception) {
	throw *driver.exception;
}
// destructor of attachment is never called


Is this a known bug in the compiler, and are the symptoms documented
somewhere? If this has been fixed, which version of the compiler
works?

If this is not a bug in the compiler, how can we fix this?
david hoke

Posts: 616
Registered: 2/9/07
Re: Destructor is not always called after exception is thrown in the scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2015 6:47 AM   in response to: david hoke in response to: david hoke
Looking again, I guess example 3 is local destructor that apparently is
being called (to release the ScopeGuard held by boost::shared_ptr)...

david hoke wrote:

Oops, also failed to ask:
Is it evident only with 'locally' defined objects, as I believe all of
your examples are?
That might come out if you look at the links, but should have
asked/highlighted in previous message...

david hoke wrote:

I have no solutions for you, but some thoughts/questions, and some
possibly similar old history...
history:
http://www.borlandtalk.com/temporarily-missing-local-object-destructor-calls-bad-co-vt99850.html
http://qc.embarcadero.com/wc/qcmain.aspx?d=33712
http://www.delphigroups.info/3/8/117547.html

questions:
1)Are you using classic or clang?
2)If classic, have you tried the 32bit clang compiler (seems
problematic for some, but might try)
3)Have you tried 'release' build, but telling it to generate
debugging information?

I wonder if they never actually got rid of the switch controlling
local destructor generation internally, but maybe just took away
command line parameters accessing it...

Ville-Valtteri Tiittanen wrote:

Hello, we are experiencing a problem in which every instance in a
scope is not destructed after an exception is thrown. This problem
occurs only in Release build, and never in Debug build. What seems
to make difference are debug symbols; for example a Debug build
with all optimizations work fine.

We are using Embarcadero C++ Builder XE8 version 22.0.19027.8951,
TwineCompile 4.2.0.47, and Boost 1.39.0. Target platform is 32-bit
Windows and development platform is 64-bit Windows 7 sp 1.
Language standard is C++03.

The following code shows the problem and our workaround:

Definition of ScopeGuard:
	struct ScopeGuard : boost::noncopyable{
	public:
		typedef boost::function<void(void)> function_t;
		ScopeGuard(function_t f) : f(f) {}
		~ScopeGuard() {
			try{
				f();
			} catch(...) {}
		}
 
	private:
		function_t f;
	};
 
Example 1:
 
	ScopeGuard sg(function);
	try{
		throw OurException();
	}
	catch(...){
		throw;
	} // destructor of sg is never called
 
Example 2:
 
	ScopeGuard sg(function);
	try{
		throw OurException();
	}
	catch(const OurException& e){
		throw;
	} // destructor of sg is always called
 
Example 3:
 
	boost::shared_ptr<ScopeGuard> sg(new ScopeGuard(function));
	try{
		throw OurException();
	}
	catch(...){
		throw;
	} // destructor of sg is always called


The problem exists at least with the ScopeGuard class. Under some
circumstances, even destructors of boost::shared_ptr are not
called, but I have not been able to create minimal working
examples for that. Something similar to this triggers the
problem:

struct Driver {
	boost::shared_ptr<OurException> exception;
} driver;
 
boost::shared_ptr<Attachment> attachment = createAttachment();
 
if (driver.exception) {
	throw *driver.exception;
}
// destructor of attachment is never called


Is this a known bug in the compiler, and are the symptoms
documented somewhere? If this has been fixed, which version of
the compiler works?

If this is not a bug in the compiler, how can we fix this?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Destructor is not always called after exception is thrown in thescope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2015 10:20 AM   in response to: david hoke in response to: david hoke
david wrote:

1)Are you using classic or clang?

2)If classic, have you tried the 32bit clang compiler (seems
problematic for some, but might try)

Ville is using XE8 compiling for 32bit, so the classic compiler is being
used as there is no 32bit clang compiler in XE8, that is new in Seattle.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Destructor is not always called after exception is thrown in the scope
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2015 10:24 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
Ville-Valtteri wrote:

Is this a known bug in the compiler

It was in earlier versions. Would have expected it to be fixed by now.
But I see this QC ticket is still open:

destructors for local objects not invoked
http://qc.embarcadero.com/wc/qcmain.aspx?d=33712

If this is not a bug in the compiler, how can we fix this?

Based on past discussions, try restarting the IDE and then do a full rebuild
of the project.

--
Remy Lebeau (TeamB)
Ville-Valtteri ...

Posts: 13
Registered: 6/18/15
Re: Destructor is not always called after exception is thrown in the scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2015 11:49 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Ville-Valtteri wrote:

Is this a known bug in the compiler

It was in earlier versions. Would have expected it to be fixed by now.
But I see this QC ticket is still open:

destructors for local objects not invoked
http://qc.embarcadero.com/wc/qcmain.aspx?d=33712

If this is not a bug in the compiler, how can we fix this?

Based on past discussions, try restarting the IDE and then do a full rebuild
of the project.

--
Remy Lebeau (TeamB)

Opening and closing IDE did not fix this. I tested using code block 2 from my original post (destructor of boost::shared_ptr not being called).

I went to IDE which had been open for 15 minutes and changed Build Configuration from Debug to Release. Built project. The problem persisted.

Closed IDE, opened IDE, and rebuilt. The problem persisted.

Disabled TwineCompile, closed IDE, killed all compiler processes, opened IDE, and rebuilt without TwineCompile. The problem persisted.

I did not try to reboot the computer.

Typical command line:

c:\program files (x86)\embarcadero\studio\16.0\bin\bcc32.exe -DNDEBUG;BOOST_THREAD_USE_LIB
-n.\Win32\Release -I"(removed)" -y -Q -c -Vmp -tM -tU -tW -C8 -o.\Win32\Release\(removed).obj -wamb -wasm -wbbf -wdef -w-par -wstu
  -wprc -wstl -wntn -wiac -wbcx -wpun -O2 -v -vi -H=.\Win32\Release\(removed).pch -H (removed).cpp 
david hoke

Posts: 616
Registered: 2/9/07
Re: Destructor is not always called after exception is thrown in the scope [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 14, 2015 6:40 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
Ville-Valtteri Tiittanen wrote:

Remy Lebeau (TeamB) wrote:
Ville-Valtteri wrote:

Is this a known bug in the compiler

It was in earlier versions. Would have expected it to be fixed by
now. But I see this QC ticket is still open:

destructors for local objects not invoked
http://qc.embarcadero.com/wc/qcmain.aspx?d=33712

If this is not a bug in the compiler, how can we fix this?

Based on past discussions, try restarting the IDE and then do a
full rebuild of the project.

--
Remy Lebeau (TeamB)

Opening and closing IDE did not fix this. I tested using code block 2
from my original post (destructor of boost::shared_ptr not being
called).

Unless I am mistaken, it appears that...

In your original post, "example 2" ('code block 2'?) did not use boost,
and comment stated that destructor was always called.

Also there, "example 3" did use boost, but comment for that also stated
that destructor was always called.

Clarification may not matter, but thought I'd point this out.
Ville-Valtteri ...

Posts: 13
Registered: 6/18/15
Re: Destructor is not always called after exception is thrown in the scope [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 14, 2015 10:28 PM   in response to: david hoke in response to: david hoke
{quote:title=david hoke wrote:}
Unless I am mistaken, it appears that...

In your original post, "example 2" ('code block 2'?) did not use boost,
and comment stated that destructor was always called.

Also there, "example 3" did use boost, but comment for that also stated
that destructor was always called.

Clarification may not matter, but thought I'd point this out.

My original post has two code blocks (I am referring to forum markup). First code block is one with three examples, and second code block is one that I tested now.
david hoke

Posts: 616
Registered: 2/9/07
Re: Destructor is not always called after exception is thrown in the scope [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 15, 2015 7:17 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
Ville-Valtteri Tiittanen wrote:

My original post has two code blocks (I am referring to forum
markup). First code block is one with three examples, and second code
block is one that I tested now.

Ok, didn't catch that in newsreader (altho it is there when looking for
it.)

Does it behave any differently if you use

boost::shared_ptr<Attachment> attachment(createAttachment());

[
instead of

boost::shared_ptr<Attachment> attachment = createAttachment();
]

?

I ask since you indicated that form in your other code block
(ScopeGuard items) did cause destructor to be called...
Vladimir Ulchenko

Posts: 248
Registered: 1/12/00
Re: Destructor is not always called after exception is thrown in the scope  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 13, 2015 11:53 PM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
On Tue, 13 Oct 2015 05:02:29 -0700, Ville-Valtteri Tiittanen <> wrote:

Is this a known bug in the compiler, and are the symptoms documented somewhere? If this has been fixed, which version of the compiler works?

exception handling in bcc is broken as always

If this is not a bug in the compiler, how can we fix this?

I think the only reliable way would be to abandon bcb

--
Vladimir Ulchenko aka vavan
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02