By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

Stack unwinding and destructors?

P: n/a
On page 366 of TC++PL(SE) we are told " The destructor will be called
independently of whether the function is exited normally or exited because
an exception is thrown."

On page 382 the following appears:

"Consider the simple function f() that appears to have nothing to do with
exception handling:

void g(int);

void f()
{
string s;
//...
g(1);
g(2);
}

However, g() may throw an exception, so f() must contain code ensuring that
s is destroyed correctly in case of an exception."

These statements seem mutually inconsistent. Would it have been more
correct to use 'string* s = new string();' in place of "string s;"?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 09 Aug 2004 14:07:46 -0400, Steven T. Hatton
<su******@setidava.kushan.aa> wrote:
On page 366 of TC++PL(SE) we are told " The destructor will be called
independently of whether the function is exited normally or exited
because
an exception is thrown."

On page 382 the following appears:

"Consider the simple function f() that appears to have nothing to do with
exception handling:

void g(int);

void f()
{
string s;
//...
g(1);
g(2);
}

However, g() may throw an exception, so f() must contain code ensuring
that
s is destroyed correctly in case of an exception."
I think 'must contain code' could be better expressed as 'the compiler
must place code'. There is nothing that the programmer need do to ensure
that the destructor for s is called, either by stack unwinding or normal
function exit.

These statements seem mutually inconsistent. Would it have been more
correct to use 'string* s = new string();' in place of "string s;"?


I don't think so, that would be a guaranteed memory leak, however the
function was exitted.

john
Jul 22 '05 #2

P: n/a
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in
news:0O********************@speakeasy.net:
On page 366 of TC++PL(SE) we are told " The destructor will be called
independently of whether the function is exited normally or exited
because an exception is thrown."
Yep
On page 382 the following appears:

"Consider the simple function f() that appears to have nothing to do
with exception handling:

void g(int);

void f()
{
string s;
//...
g(1);
g(2);
}

However, g() may throw an exception, so f() must contain code ensuring
that s is destroyed correctly in case of an exception."

These statements seem mutually inconsistent. Would it have been more
correct to use 'string* s = new string();' in place of "string s;"?


More correct in what way? And how are the statements mutually
inconsistent?
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.