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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,2153d570c2f03e29,start X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.66.89.39 with SMTP id bl7mr2042763pab.33.1355542700834; Fri, 14 Dec 2012 19:38:20 -0800 (PST) Received: by 10.50.191.131 with SMTP id gy3mr1350232igc.1.1355542700791; Fri, 14 Dec 2012 19:38:20 -0800 (PST) Path: s9ni44656pbb.0!nntp.google.com!kr7no2750654pbb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Fri, 14 Dec 2012 19:38:20 -0800 (PST) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=118.6.198.203; posting-account=Mi71UQoAAACnFhXo1NVxPlurinchtkIj NNTP-Posting-Host: 118.6.198.203 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Question about library-level functions From: ytomino Injection-Date: Sat, 15 Dec 2012 03:38:20 +0000 Content-Type: text/plain; charset=ISO-8859-1 Date: 2012-12-14T19:38:20-08:00 List-Id: Hello. What's the difference between a library-level(top-level?) function(A) and a function(B) in a library-level package? with Something; function A return access T; -- returns new allocated value with Something; package P is function B return access T; -- same as above end P; I had thought these are same. But, returned value from A seems that's finalized inside A. B is not. Does the master of a library-level function happen to exist inside itself? My test case is below: ---- %< ---- with Ada.Finalization; package lifetime is type T is limited private; function Create return T; private type T is limited new Ada.Finalization.Limited_Controlled with null record; overriding procedure Finalize (Object : in out T); end lifetime; with Ada.Text_IO; with System.Address_Image; package body lifetime is procedure Finalize (Object : in out T) is begin Ada.Text_IO.Put_Line ("Finalize (" & System.Address_Image (Object'Address) & ")"); end Finalize; function Create return T is begin return Result : T := (Ada.Finalization.Limited_Controlled with null record) do Ada.Text_IO.Put_Line ("Initialize (" & System.Address_Image (Result'Address) & ")"); end return; end Create; end lifetime; with lifetime; function alloc return access lifetime.T; function alloc return access lifetime.T is begin return new lifetime.T'(lifetime.Create); -- lifetime.Finalize may be called here end alloc; with Ada.Text_IO; with lifetime; with alloc; procedure main is begin Ada.Text_IO.Put_Line ("before"); declare type A is access all lifetime.T; P : A := A(alloc); begin -- P.all may be already finalized!!! Ada.Text_IO.Put_Line ("lifetime"); end; Ada.Text_IO.Put_Line ("after"); end main; ---- >% ---- % gnatmake -gnat2012 main.adb && ./main before Initialize (0000000100100090) Finalize (0000000100100090) <- !!!! lifetime after When I tried to move 'alloc' to inside of any package, the calling lifetime.Finalize in 'alloc' would not be invoked. It's natural.