From: mheaney@on2.com (Matthew Heaney)
Subject: Re: virtual destructors
Date: 23 Apr 2003 08:16:44 -0700
Date: 2003-04-23T15:16:45+00:00 [thread overview]
Message-ID: <1ec946d1.0304230716.11112922@posting.google.com> (raw)
In-Reply-To: u3ckacuwv.fsf@nasa.gov
Stephen Leake <Stephe.Leake@nasa.gov> wrote in message news:<u3ckacuwv.fsf@nasa.gov>...
> "kat-Zygfryd" <6667@wp.pl> writes:
>
> Yes. The compiler ensures that Finalize is called. Users will forget
> to call Dispose.
Another possibility is to use a handle of some kind (a la the C++
auto_ptr class), that claims ownership of the access object, and
automatically calls Free when the handle object is itself finalized.
The Charles library has an Access_Control package that does just that,
e.g.
declare
P1 : Pointer_Type; --from Charles.Access_Control
P2 : Pointer_Type;
begin
Initialize (P1, new T); --claim ownership
Assign (Target => P2, Source => P1); --transfer ownership
Op (+P2); --use access object
end; --P2 will Free object
You can even use this in a factory function:
function New_T return T_Access is
P : Pointer_Type;
begin
Initialize (P, new T); --claim ownership
--do some stuff that may raise an exception
return Release (P); --relinguish ownership
end;
If the factory function New_T raises an exception prior to reaching
the return, then pointer P will free the allocated object during its
finalization, because it still owns the object.
If we do reach the return statement, then we tell P to release the
object, so P does nothing during its finalization, and ownership of
the allocated object is transfered to the caller of New_T.
The party to whom New_T transfers ownership might indeed even be
another access control object:
declare
P : Pointer_Type;
begin
Initialize (P, Value => New_T);
--now use +P
end; --free object during finalization of P
The Charles.Access_Control component is part of the Charles library,
available at my website:
http://home.earthlink.net/~matthewjheaney/charles/
next prev parent reply other threads:[~2003-04-23 15:16 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-04-22 14:18 virtual destructors kat-Zygfryd
2003-04-22 14:27 ` Stephen Leake
2003-04-22 14:56 ` kat-Zygfryd
2003-04-22 15:45 ` kat-Zygfryd
2003-04-22 16:34 ` tmoran
2003-04-22 21:32 ` Robert A Duff
2003-04-22 17:18 ` Stephen Leake
2003-04-22 16:34 ` Simon Wright
2003-04-22 19:57 ` Stephen Leake
2003-04-22 20:19 ` Simon Wright
2003-04-22 21:23 ` Robert A Duff
2003-04-23 15:16 ` Matthew Heaney [this message]
2003-04-22 17:31 ` virtual destructors - doesn't seem to work kat-Zygfryd
2003-04-22 17:32 ` Simon Wright
2003-04-22 17:33 ` virtual destructors Matthew Heaney
2003-04-22 17:28 ` Matthew Heaney
2003-04-22 17:40 ` kat-Zygfryd
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox