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,3fc1c2283df835d5 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-30 05:57:33 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!news-FFM2.ecrc.net!news.iks-jena.de!not-for-mail From: Lutz Donnerhacke Newsgroups: comp.lang.ada Subject: Re: Limited_Controlled types as 'out' arguments Date: Wed, 30 Jul 2003 12:57:32 +0000 (UTC) Organization: IKS GmbH Jena Message-ID: References: NNTP-Posting-Host: taranis.iks-jena.de X-Trace: branwen.iks-jena.de 1059569852 12670 217.17.192.37 (30 Jul 2003 12:57:32 GMT) X-Complaints-To: usenet@iks-jena.de NNTP-Posting-Date: Wed, 30 Jul 2003 12:57:32 +0000 (UTC) User-Agent: slrn/0.9.7.4 (Linux) Xref: archiver1.google.com comp.lang.ada:41004 Date: 2003-07-30T12:57:32+00:00 List-Id: * Matthew Heaney wrote: > Controlled types, like all tagged types, are passed by reference. 'out' Parameters can't be read. > Three objects (a, b, c) are Initialize'd , and the same three objects are > Finalize'd. > > Which objects do you think aren't being finalized? Extended example: ------------------------------------------------------------------------ with Ada.Finalization; package t1 is type Char_Access is access Character; type Test is new Ada.Finalization.Limited_Controlled with record a : Char_Access; end record; procedure Initialize(o : in out Test); procedure Finalize(o : in out Test); procedure Copy(to : out Test; from : Test); end t1; ------------------------------------------------------------------------ with t1; use t1; procedure t is a, b, c : Test; begin Copy(a, b); Copy(a, c); end t; ------------------------------------------------------------------------ with Ada.Text_IO; with System.Storage_Elements, System.Address_To_Access_Conversions; with Unchecked_Deallocation; use Ada.Text_IO; package body t1 is procedure Debug (msg : String; p : Char_Access) is use System.Storage_Elements; package Convert is new System.Address_To_Access_Conversions(Character); begin Put_Line(msg & Integer_Address'Image(To_Integer( Convert.To_Address(Convert.Object_Pointer(p)))) & '(' & p.all & ')'); end Debug; global : Character := '0'; procedure Initialize(o : in out Test) is begin o.a := new Character'(global); Debug("Initializing", o.a); global := Character'Succ(global); end Initialize; procedure Finalize(o : in out Test) is procedure Free is new Unchecked_Deallocation(Character, Char_Access); begin Debug("Finalizing", o.a); Free(o.a); end Finalize; procedure Copy(to : out Test; from : Test) is begin to.a := new Character'(global); Debug("Copying from", from.a); Debug("Copying to ", to.a); global := Character'Succ(global); end Copy; end t1; ------------------------------------------------------------------------ Results in a beautiful memory leak: Initializing 134630832(0) Initializing 134630848(1) Initializing 134630864(2) Copying from 134630848(1) Copying to 134630880(3) Copying from 134630864(2) Copying to 134630896(4) Finalizing 134630864(2) Finalizing 134630848(1) Finalizing 134630896(4) 134630832 and 134630880 are never freed.