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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,714a9fe4718c9803 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!proxad.net!newsfeed.stueberl.de!npeer.de.kpn-eurorings.net!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Problems with controlled types, gnatmem thinks handle is leaking memory (long) Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Sun, 20 Feb 2005 19:09:02 +0100 Message-ID: <1s73d5wqz8pme.1m22aqftdn6ij.dlg@40tude.net> NNTP-Posting-Date: 20 Feb 2005 19:09:00 MET NNTP-Posting-Host: 9248418a.newsread2.arcor-online.net X-Trace: DXC=GDkg2JNaho7GKQ1;SL\gZ1Q5U85hF6f;4jW\KbG]kaM8ea\9g\;7Nm5lEB7XVY=6:>[6LHn;2LCV>COgUkn_?_Y?d]IVo:ABZh7 X-Complaints-To: abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:8432 Date: 2005-02-20T19:09:00+01:00 List-Id: On Sun, 20 Feb 2005 16:30:14 +0000, Luke A. Guest wrote: > I need to implement a "smart pointer" in Ada and I did a few months ago > write one based on a few sources, but it had two levels of indirection in > it, so I looked at the Handles examples by Matthew Heaney on AdaPower. > > I modified my code to have only one level, and an aliased item in my > "node" but according to gnatmem it leaks (now I remember running > gnatmem on the old one and it came out with similar results). > > I tried this with GNAT-3.15p. I also copied over my test code to use with > Matthew's code, and it produces the same results, leaks. Is this a problem > with gnatmem? > > Another thing is that there seems to be more calls to Adjust and Finalize > than I expect. > > Can someone help me out? I'll post my code rather than Matthew's. > > At the end, I've copied the output of the program, followed by an > annotated log where I've added comments and questions. > > Thanks to everyone who helps, > Luke. > > [...] The code does not much differ in implementation from mine (http://www.dmitry-kazakov.de/ada/components.htm). Minor comments: In Finalize: I check if Ref_Count = 0 before decrementing and raise Program_Error rather than Constraint_Error. When you call Unchecked_Dellocation it sets the pointer to null. You don't need to do that again. I also have the pointed objects limited controlled and check if the Ref_Count is zero upon object's finalization. That helps a lot! Especially if you mistakenly create circular dependencies in the objects. (The compiler will finalize them anyway so you'll get an exception) > -- Annotated Output.log > Create: Enter > Create: Exit > > ** This Adjust is the assignment on the line where the Create is called, > ** inside the Create function the Ref_Count would be 1 > Adjust: Enter > Adjust: Self.Handle_Data.Ref_Count = 2 > Adjust: Exit -- The result of Create is adjusted > > ** This Finalize is obviously the local object inside Create > Finalize: Enter > Finalize: Self.Handle_Data.Ref_Count = 1 > Finalize: Exit -- The aggregate in Create is finalized > ** What is this Finalize, Adjust, Finalize? > Finalize: Enter > Finalize: Exit -- Test is finalized (it points to nothing) > Adjust: Enter > Adjust: Self.Handle_Data.Ref_Count = 2 > Adjust: Exit -- Test is adjusted > Finalize: Enter > Finalize: Self.Handle_Data.Ref_Count = 1 > Finalize: Exit -- Create's result is finalized > > ** Now we're back at the two references, this is fine. > Ref: Enter > Ref: Enter > A: Ref: Enter > 6 B: Ref: Enter > 12.34 > > ** This Finalize is for Test2 > Finalize: Enter > Finalize: Exit > > ** The object from Test has been copied to Test2 and has been Adjusted > Adjust: Enter > Adjust: Self.Handle_Data.Ref_Count = 2 > Adjust: Exit > > ** This would be the Finalization of both Test & Test2 on exit > Finalize: Enter > Finalize: Self.Handle_Data.Ref_Count = 1 > Finalize: Exit > > ** Notice this actually deletes the memory, so gnatmem is wrong here! > Finalize: Enter > Finalize: Self.Handle_Data.Ref_Count = 0 > Finalize: Deallocated memory > Finalize: Exit Everything seems OK to me. You can make Handle_Type controlled add Initialize and Finalize to see when they are called. Handle_Type.Finalize should be called *before* the text Finalize: Deallocated memory Otherwise, if you really had a leak, it would be called after that. (The list of all controlled objects is maintained at run-time.) -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de