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-Thread: 103376,c4cb2c432feebd9d X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!grolier!usenet-fr.net!nerim.net!news.cs.univ-paris8.fr!u-psud.fr!not-for-mail From: Philippe Tarroux Newsgroups: comp.lang.ada Subject: Re: Finalization Date: Tue, 21 Nov 2006 11:32:31 +0100 Organization: University Paris-Sud, France. Message-ID: References: <0ugu4e.4i7.ln@hunter.axlog.fr> <%P_cg.155733$eR6.26337@bgtnsc04-news.ops.worldnet.att.net> <6H9dg.10258$S7.9150@news-server.bigpond.net.au> <1hfv5wb.1x4ab1tbdzk7eN%nospam@see.signature> <2006052509454116807-gsande@worldnetattnet> NNTP-Posting-Host: osiris.limsi.fr Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: upsn250.cri.u-psud.fr 1164105210 3045 129.175.157.197 (21 Nov 2006 10:33:30 GMT) X-Complaints-To: newsmaster@u-psud.fr NNTP-Posting-Date: Tue, 21 Nov 2006 10:33:30 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; fr-FR; rv:1.7.8) Gecko/20050511 X-Accept-Language: fr, en In-Reply-To: Xref: g2news2.google.com comp.lang.ada:7597 Date: 2006-11-21T11:32:31+01:00 List-Id: >Note that Obj is declared Controlled, not Limited_Controlled, therefore the >function Process would make a copy of it. > If I declare it Limited_Controlled I can't use the function version anymore. Right? >When you are dealing with copies >of pointers (the field X), you should decide what you would do with >multiple pointers to the same object, especially upon finalization. In any >case you have to override Adjust (and probably use reference counting). But >I suppose it should better be Limited_Controlled. > > I suppose too. It is what I observed: it works with the procedure version. >> Free (O.X); >> end Finalize; >> >> procedure Process (O : in out Obj) is >> begin >> Text_Io.Put ("In process procedure"); Text_Io.New_Line; >> Finalize(O); >> >> > >You don't need that. It breaks your design. As a rule, Finalize should be >called once, and almost never explicitly. > > Yes. I added it only to see what happened here. Of course it is useless. >> O.X := new Vector (1 .. 100); >> end Process; >> >> function Process return Obj is >> O : Obj; >> begin >> Text_Io.Put ("In process function"); Text_Io.New_Line; >> O.Process; >> return O; >> end Process; >> >> > >This is broken. You allocate a new vector in Process, then a shallow copy >of is made in "return 0." Then O is destructed and as a result Finalize >kills that vector. The caller receives the shallow copy object with a >dangling pointer in the filed X. > > But I don't undestand why it works for the first 7 iteration of the loop and crashes only at the 8th. >Do it this way: > > for I in 1 .. 100 loop > declare > O : Obj; -- Ideally Initialize should allocate the vector > begin > Process (O); -- Don't allocate anything here > > You mean that there should be no allocation here? vector is allocated inside process. > end; -- Finalize takes care of vector > end loop; > But anyway thank you for these explanations that clarify the point. Philippe Tarroux