comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Problems with controlled types, gnatmem thinks handle is leaking memory (long)
Date: Sun, 20 Feb 2005 19:09:02 +0100
Date: 2005-02-20T19:09:00+01:00	[thread overview]
Message-ID: <1s73d5wqz8pme.1m22aqftdn6ij.dlg@40tude.net> (raw)
In-Reply-To: pan.2005.02.20.16.30.10.917724@n_o_p_o_r_k_a_n_d_h_a_m.abyss2.demon.co.uk

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.
> 
> <code>
[...]

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



  reply	other threads:[~2005-02-20 18:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-20 16:30 Problems with controlled types, gnatmem thinks handle is leaking memory (long) Luke A. Guest
2005-02-20 18:09 ` Dmitry A. Kazakov [this message]
2005-02-20 23:09   ` Luke A. Guest
2005-02-21  8:49     ` Dmitry A. Kazakov
2005-02-21 20:12     ` Simon Wright
2005-02-21 20:54       ` Dmitry A. Kazakov
2005-02-21 20:59         ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox