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: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Brian Drummond Newsgroups: comp.lang.ada Subject: Re: Why is the destructor called multiple times after I declare an object? Date: Mon, 11 Jan 2016 16:29:44 -0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: <293c58ac-4ebd-488a-abcc-b6e88811eec8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 11 Jan 2016 16:29:44 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="da745e888d4a5182b5fda6212bbb0a63"; logging-data="14743"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18MJNtmisxsf/g6Lg+YApu7YO9QNowgFac=" User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Cancel-Lock: sha1:mHm1+PTdwqtRPvaopvVgy8WiteQ= Xref: news.eternal-september.org comp.lang.ada:29087 Date: 2016-01-11T16:29:44+00:00 List-Id: On Sun, 10 Jan 2016 19:18:53 -0700, Jeffrey R. Carter wrote: > On 01/10/2016 06:37 PM, Andrew Shvets wrote: >> >> Why is "Resetting values of Creat to defaults." displaying 6 times as >> opposed to just twice? > > ARM 7.6(13-17.1) apply here. Note that Init declares a local variable of > the type, which is finalized when Init returns, and that the anonymous > object that is the result of the call to Init is finalized after > initialization is finished. > This gives you 2 calls to Finalize for each variable of the type > declared and initialized by a call to Init. Finally you have a call to > Finalize for each variable of the type when the program ends. > > http://www.adaic.org/resources/add_content/standards/12rm/html/ RM-7-6.html Interesting. I thought you could eliminate one of those local copies using "extended return" in the Init function, as in: function Init return Creature is begin return TempCreature : Creature do TempCreature.Name := Ada.Strings.Unbounded.To_Unbounded_String("dog"); TempCreature.Legs := 4; TempCreature.WeightInGrams := 3000; TempCreature.HeightInCm := 40; end return; end Init; Somehow I expected "extended return" to allocate space and "build in place" during the execution of the return statement. Is this something legal that Gnat doesn't take advantage of? Or is there a Gnat option I'm not aware of? Of course instead of a default constructor, the record can be populated with default values, to eliminate both the "Init" call and two temporary objects, but the "problem" remains for the other constructor. -- Brian