From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,de1c23707584fc3c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-04-23 08:16:45 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: mheaney@on2.com (Matthew Heaney) Newsgroups: comp.lang.ada Subject: Re: virtual destructors Date: 23 Apr 2003 08:16:44 -0700 Organization: http://groups.google.com/ Message-ID: <1ec946d1.0304230716.11112922@posting.google.com> References: NNTP-Posting-Host: 66.162.65.162 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1051111005 7810 127.0.0.1 (23 Apr 2003 15:16:45 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 23 Apr 2003 15:16:45 GMT Xref: archiver1.google.com comp.lang.ada:36411 Date: 2003-04-23T15:16:45+00:00 List-Id: Stephen Leake wrote in message news:... > "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/