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,56131a5c3acc678e X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-12-04 00:51:33 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!tar-atanamir.cbb-automation.DE!not-for-mail From: Dmitry A. Kazakov Newsgroups: comp.lang.ada Subject: Re: Question about OO programming in Ada Date: Thu, 04 Dec 2003 09:55:31 +0100 Message-ID: <2prtsvgmt5lt3u1ulb5dvh8ba5nulfl3l3@4ax.com> References: <8urxb.19482$sb4.18182@newsread2.news.pas.earthlink.net> <1792884.HtYz4Yv8lY@linux1.krischik.com> <1070466281.168920@master.nyc.kbcfp.com> <1070490862.478119@master.nyc.kbcfp.com> NNTP-Posting-Host: tar-atanamir.cbb-automation.de (212.79.194.116) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 1070527892 71774861 212.79.194.116 ([77047]) X-Newsreader: Forte Agent 1.8/32.548 Xref: archiver1.google.com comp.lang.ada:3125 Date: 2003-12-04T09:55:31+01:00 List-Id: On Wed, 03 Dec 2003 20:23:53 -0500, "Robert I. Eachus" wrote: >Hyman Rosen wrote: > >> Not on a pointer field within the object, but on the object itself. > >I'm very confused about why Hyman is confused. Making a copy of an >access value in Ada, and calling Free for both the original and the copy >is bad juju. Calling Free twice, or twenty times and passing the only >the original access object is never a problem. It is worth to write a sample code to the problem: with Ada.Finalization; package B is type Object is new Ada.Finalization.Limited_Controlled with record Got_It : Boolean := False; -- Prevents recursion end record; procedure Finalize (X : in out Object); end B; with Ada.Unchecked_Deallocation; with Text_IO; use Text_IO; package body B is type Object_Ptr is access all Object'Class; procedure Free is new Ada.Unchecked_Deallocation (Object'Class, Object_Ptr); procedure Finalize (X : in out Object) is Ptr : Object_Ptr := X'Unchecked_Access; begin if not X.Got_It then X.Got_It := True; Put_Line ("Deallocation"); Free (Ptr); end if; end Finalize; end B; Intended to mend this: with B; use B; with Text_IO; use Text_IO; procedure Test1 is begin Put_Line ("Begin of the scope"); declare type Pointer is access Object; X : Pointer; begin X := new Object; -- This is not dangled! end; Put_Line ("End of the scope"); end Test1; The point is that calling Free from Finalize is revolting. This would not be necessary if finalization of the type Pointer called deallocator in addition to a call to Finalize. -- Regards, Dmitry Kazakov http://www.dmitry-kazakov.de