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=unavailable autolearn_force=no version=3.4.4 Path: border1.nntp.dca1.giganews.com!nntp.giganews.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!fx29.iad.POSTED!not-for-mail From: Brad Moore User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: newbie: can't read fast enough... :-) memory leaks... References: <479b2efe-0238-4b2a-8b05-cb1a0b4a57e5@googlegroups.com> <1409739464.7121.235.camel@obry.net> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 68.145.219.148 X-Complaints-To: internet.abuse@sjrb.ca X-Trace: 1409840630 68.145.219.148 (Thu, 04 Sep 2014 14:23:50 UTC) NNTP-Posting-Date: Thu, 04 Sep 2014 14:23:50 UTC Date: Thu, 04 Sep 2014 08:23:50 -0600 X-Received-Bytes: 2845 X-Received-Body-CRC: 2129297043 X-Original-Bytes: 2763 Xref: number.nntp.dca.giganews.com comp.lang.ada:188864 Date: 2014-09-04T08:23:50-06:00 List-Id: On 2014-09-03 11:32 PM, Jeffrey Carter wrote: > On 09/03/2014 09:57 PM, Brad Moore wrote: >> >> declare >> type String_Access is access String; >> A, B : String_Access; >> begin >> A := new String'("String 1"); >> B := new String'("String 2"); >> ... -- Operate on the data. >> end; >> -- A and B are automatically freed here because the String_Access >> -- is finalized since the scope of the declaration of the access >> -- type is being exited, and finalizing an access type also >> -- finalizes its collection (allocated objects designated by that >> -- type). >> >> This is a nice way to free up a bunch of objects that are no longer needed, if >> it can be guaranteed that the objects are only needed in a nested scope. I like >> this better than garbage collection, because the >> deallocation occurs more consistently, when and where it is needed. >> I view this as being less error prone, if one is able to take advantage of this >> approach. > > Recent comments here in another thread indicate that this only works if > 'Storage_Size is defined for the access type. > That doesn't appear to be correct. In the latest GNAP GPL, the following program outputs; Finalizing Finalizing Exited Block with Ada.Text_IO; use Ada.Text_IO; with Ada.Finalization; use Ada; procedure Test_Deallocation is begin declare type T is new Finalization.Limited_Controlled with null record; overriding procedure Finalize (Object : in out T); overriding procedure Finalize (Object : in out T) is begin Put_Line ("Finalizing "); end Finalize; type T_Access is access T; T1, T2 : T_Access; begin T1 := new T; T2 := new T; end; Put_Line ("Exited Block"); end Test_Deallocation;