Re: exceptions & dataflow

Jerry Leichter <>
14 Feb 1998 14:41:07 -0500

          From comp.compilers

Related articles
[10 earlier articles]
Re: exceptions & dataflow (Jason Merrill) (1998-02-12)
Re: exceptions & dataflow (Fergus Henderson) (1998-02-12)
Re: exceptions & dataflow (David Chase) (1998-02-12)
Re: exceptions & dataflow (Amit Bhatnagar) (1998-02-12)
Re: exceptions & dataflow (David L Moore) (1998-02-14)
Re: exceptions & dataflow (Sergey Solyanik) (1998-02-14)
Re: exceptions & dataflow (Jerry Leichter) (1998-02-14)
| List of all articles for this month |

From: Jerry Leichter <>
Newsgroups: comp.compilers
Date: 14 Feb 1998 14:41:07 -0500
Organization: System Management ARTS
References: 98-02-055
Keywords: dataflow, C++

| I accidently found out that within a block the destructors of the
| objects inside that block are NOT called in C++. They are only called
| when the stack for the function call winds up. Do u think this is a
| good approach?

Any compiler that does this is broken. C++ requires that destructors
for local variables run as soon as their block terminates.

There's a common idiom that absolutely depends on this, known as
"allocation is resource acquisition". In this idiom, the constructor
of a class allocates the resource, and the destructor deallocates it.
A common "resource" is a mutex. Thus, you would do:

{ Mutex_locker l(mutex); // Locks l
} // Destructor unlocks l

This is the way C++ provides functionality similar to the Modula3/Java
try - finally block: You're guaranteed that the destructor will run,
and the mutex will be released, no matter how you leave the block
(e.g., through an exception).

An implementation that put of unlocking the mutex until the function
returned would simply not work: The function may very well be in a
loop, and will *never* return! (Even without considering mutexes,
think of what would happen if, in a block in a loop, you allocated
some big temporary. The temporaries would just build up

| I would prefer that all the destructors for the local block be called
| as soon as the scope of the variable is over.

That's what's required by the C++ language. Get a better compiler.

| I am not familiar with Java, but is a 'finalizer' similar kind of a
| concept?

A finalizer is invoked by the Java garbage collector. You have no
idea when the garbage collector will run, when it will find the object
is no longer accessible, or when your finalizer will run. So, while
related, a finalizer cannot be used for some purposes a destructor can

-- Jerry

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.