comp.lang.ada
 help / color / mirror / Atom feed
* Memory leak - What the ...?
@ 2004-10-10 21:33 Alex R. Mosteo
  2004-10-10 22:05 ` Marius Amado Alves
                   ` (3 more replies)
  0 siblings, 4 replies; 51+ messages in thread
From: Alex R. Mosteo @ 2004-10-10 21:33 UTC (permalink / raw)


Hi, 

as the topic says, this post is about some code which leaks. I'm now
sure of having trapped the leak, but I don't understand where is my
error.

The culprit is a controlled type, one of whose members is an access to
a Stream_Element_Array. The idea is to have an array of the exact
length required and not a worst case one, without using an
unconstrained type.

Platform is gnat 3.15p, compiling with assertions enabled (-gnata),
either win32 or linux.

Running a lot of tests with gnatmem, it always shows the allocation
not deallocated being at:

s-assert.adb:46                        
system.assertions.raise_assert_failure
adagio-g2-transceiver_types.adb:134    
adagio.g2.transceiver_types.adjust
<other stack calls not meaningful>

I don't know what the assertion means there, since I never get any
exception anyway.

The type declaration is:

   type Udp_message is new Finalization.Controlled with record
      Data : Stream_Element_Array_Access;
      Dest : Gnat.Sockets.Sock_addr_type;
      Date : Calendar.Time;
   end record;

The adjust procedure is:

   procedure Adjust (This : in out Udp_Message) is
   begin
      if This.Data /= null then
         This.Data := new Stream_Element_Array'(This.Data.all);
      end if;
   end Adjust;

And the finalize one is:

   procedure Finalize (This : in out Udp_Message) is
   begin
      Free (This.Data);
   end Finalize;

Now, where it gets interesting is that I've double and triple checked
that no objects of Udp_Message type are being leaked. Using counters
and traces I've certified that every object of that type which is
created is eventually finalized. No code mangles with the Data
pointer, so there's no outside manipulation which could cause the
leak.

Is there something flawled in this implementation? I just can't figure
why there is a leak. Changing the allocated array to a static array
removes the leak.

These objects are stored in an Ada.Containers.Doubly_Linked_List, if
that could add something.

Thanks in advance,

A. Mosteo.



^ permalink raw reply	[flat|nested] 51+ messages in thread
* Re: Memory leak - What the ...?
@ 2004-10-11  9:50 Christoph Karl Walter Grein
  0 siblings, 0 replies; 51+ messages in thread
From: Christoph Karl Walter Grein @ 2004-10-11  9:50 UTC (permalink / raw)
  To: comp.lang.ada

procedure Test is
    Empty : Udp_Message;
    Arr : array (1 .. 1000) of Udp_Message;
    -- Initialize is called for Empty and each component of Arr;
    -- does nothing since not overridden.
begin
    Text_Io.Put_Line ("Adding...");
    for I in Arr'Range loop
	Arr (I) := Create ((1 .. Stream_Element_Offset (I) =>
			       Stream_Element'First));
	-- Create an intermediate object Msg in place with Data as given.
	-- Neither Initialize nor Adjust is called.
	-- Finalize (Arr (I));  ==> Left side of assignment statement;
	--                          does nothing since Data = null.
	-- Copy Msg to Arr (I)  ==> Shallow copy.
	-- Adjust (Arr (I));    ==> Make a deep copy from This.Data.all
	--                          (this is Msg.Data).
	-- Finalize Msg         ==> Destruct the intermediate object, i.e.
	--                          de-allocate Msg.Data.
	-- I cannot see a problem here.
    end loop;

    Text_Io.Put_Line ("Deleting...");
    for I in Arr'Range loop
	Arr (I) := Empty;
	-- Finalize (Arr (I));  ==> Left side of assignment statement;
	--                          de-allocates Data.
	-- Copy Empty to Arr (I)
	-- Adjust (Arr (I));    ==> does nothing since Empty.Data = null.
    end loop;
    -- Finalize is called for each component of Arr in inverse sequence, then
    -- for Empty.
    -- Does nothing since Data = null always.
    --
    -- I can't see a problem.
exception
    when E: others =>
	Text_Io.Put_Line ("Exception: " & Exceptions.Exception_Information (E));
end Test;

________________________________________________________________
Verschicken Sie romantische, coole und witzige Bilder per SMS!
Jetzt neu bei WEB.DE FreeMail: http://freemail.web.de/?mc=021193




^ permalink raw reply	[flat|nested] 51+ messages in thread
* Re: Memory leak - What the ...?
@ 2004-10-11 10:21 Christoph Karl Walter Grein
  0 siblings, 0 replies; 51+ messages in thread
From: Christoph Karl Walter Grein @ 2004-10-11 10:21 UTC (permalink / raw)
  To: comp.lang.ada

with Ada.Text_Io;
use Ada.Text_Io;

with Ada.Unchecked_Deallocation;

package body Test_Aux is

    function Create (Data : in Stream_Element_Array) return Udp_Message is
	Msg : Udp_Message := (Ada.Finalization.Controlled with
			      Data => new Stream_Element_Array'(Data));
    begin
	Put_Line ("Create" & Stream_Element_Offset'Image (Data'Last));
	return Msg;
    end Create;

    procedure Adjust (This : in out Udp_Message) is
    begin
	if This.Data /= null then
	    Put_Line ("Adjust" & Stream_Element_Offset'Image (This.Data'Last));
	    This.Data := new Stream_Element_Array'(This.Data.all);
	else
	    Put_Line ("Adjust null");
	end if;
    end Adjust;

    procedure Finalize (This : in out Udp_Message) is
	procedure Free is new Ada.Unchecked_Deallocation
				 (Stream_Element_Array,
				  Stream_Element_Array_Access);
    begin
	if This.Data /= null then
	    Put_Line ("Finalize" &
		      Stream_Element_Offset'Image (This.Data'Last));
	else
	    Put_Line ("Finalize null");
	end if;
	Free (This.Data);
    end Finalize;

end Test_Aux;
with Ada.Exceptions;
with Ada.Streams;
use Ada.Streams;
use Ada;
with Text_Io;
use Text_Io;

with Test_Aux;
use Test_Aux;

procedure Test_It is
    Empty : Udp_Message;
    Arr : array (1 .. 2) of Udp_Message;
    -- Initialize is called for Empty and each component of Arr;
    -- does nothing since not overridden.
begin
    Text_Io.Put_Line ("Adding...");
    for I in Arr'Range loop
	Arr (I) := Create ((1 .. Stream_Element_Offset (I) =>
			       Stream_Element'First));
	-- Create 1      => Create an intermediate object Msg in place
	--                  with Data as given.
	--                  Neither Initialize nor Adjust is called.
	-- Adjust 1      => Some internal
	-- Finalize 1    =>               copying of the intermediate object
	-- Finalize null => Finalize (Arr (I)); Left side of assignment
	--                  statement; does nothing since Data = null.
	--                  Copy Msg to Arr (I) - Shallow copy.
	-- Adjust 1      => Adjust (Arr (I)); Make a deep copy from
	--                  This.Data.all (this is the intermediate Msg.Data).
	-- Finalize 1    => Finalize Msg - Destruct the intermediate object,
	--                  i.e. de-allocate Msg.Data.
	-- Create 2
	-- Adjust 2
	-- Finalize 2
	-- Finalize null
	-- Adjust 2
	-- Finalize 2
    end loop;
    -- I cannot see a problem here.

    Text_Io.Put_Line ("Deleting...");
    for I in Arr'Range loop
	Arr (I) := Empty;
	-- Finalize 1    => Finalize (Arr (I)); Left side of assignment
	--                  statement; de-allocates Data.
	--                  Copy Empty to Arr (I)
	-- Adjust null   => Adjust (Arr (I)); does nothing since
	--                  Empty.Data = null.
	-- Finalize 2
	-- Adjust null
    end loop;
    -- Finalize null
    -- Finalize null
    -- Finalize null
    -- Finalize is called for each component of Arr in inverse sequence, then
    -- for Empty.
    -- Does nothing since Data = null always.
    --
    -- I can't see a problem.
exception
    when E: others =>
	Text_Io.Put_Line ("Exception: " & Exceptions.Exception_Information (E));
end Test_It;

__________________________________________________________
Mit WEB.DE FreePhone mit hoechster Qualitaet ab 0 Ct./Min.
weltweit telefonieren! http://freephone.web.de/?mc=021201




^ permalink raw reply	[flat|nested] 51+ messages in thread
* Re: Memory leak - What the ...?
@ 2004-10-14  4:30 Christoph Karl Walter Grein
  0 siblings, 0 replies; 51+ messages in thread
From: Christoph Karl Walter Grein @ 2004-10-14  4:30 UTC (permalink / raw)
  To: comp.lang.ada

> It also leaves the last (Finalize) unaccounted for.

You always forget about the Empty constant, which has to be finalized as well.
________________________________________________________________
Verschicken Sie romantische, coole und witzige Bilder per SMS!
Jetzt neu bei WEB.DE FreeMail: http://freemail.web.de/?mc=021193




^ permalink raw reply	[flat|nested] 51+ messages in thread

end of thread, other threads:[~2004-10-18  0:33 UTC | newest]

Thread overview: 51+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-10 21:33 Memory leak - What the ...? Alex R. Mosteo
2004-10-10 22:05 ` Marius Amado Alves
2004-10-11  8:18   ` Alex R. Mosteo
2004-10-11 11:04     ` Marius Amado Alves
2004-10-11 13:02       ` Martin Krischik
2004-10-11  8:25   ` Martin Krischik
2004-10-11  8:56     ` Martin Dowie
2004-10-11 12:59       ` Martin Krischik
2004-10-11  1:40 ` Stephen Leake
2004-10-11  8:59   ` Alex R. Mosteo
2004-10-11 18:24     ` Stephen Leake
2004-10-12  3:02       ` Brian May
2004-10-12  8:45         ` Jean-Pierre Rosen
     [not found]           ` <mailman.282.1097576360.390.comp.lang.ada@ada-france.org>
     [not found]             ` <uvegkc.jrg.ln@skymaster>
2004-10-12 12:31               ` Marius Amado Alves
2004-10-12 14:47             ` Alex R. Mosteo
2004-10-12 16:05               ` Marius Amado Alves
2004-10-12 19:37                 ` Björn Persson
2004-10-12 22:10                   ` Marius Amado Alves
     [not found]                   ` <416C5646.1020506@netcabo.pt>
2004-10-13  0:17                     ` Stephen Leake
     [not found]                     ` <u655f1ng9.fsf@acm.org>
2004-10-13  6:24                       ` Marius Amado Alves
     [not found]               ` <416C00D6.90402@netcabo.pt>
2004-10-13  0:14                 ` Stephen Leake
     [not found]           ` <416BAFA4.7020400@netcabo.pt>
2004-10-13  0:07             ` Stephen Leake
2004-10-13 13:45               ` Hyman Rosen
2004-10-14  9:15                 ` Martin Krischik
2004-10-14 17:21                   ` Hyman Rosen
     [not found]             ` <uis9f1nw3.fsf@acm.org>
     [not found]               ` <mailman.301.1097650377.390.comp.lang.ada@ada-france.org>
2004-10-13  7:40                 ` Dmitry A. Kazakov
2004-10-13 17:44                   ` Mark Lorenzen
2004-10-14  8:03                     ` Dmitry A. Kazakov
2004-10-18  0:33           ` Brian May
2004-10-12 12:05         ` Alex R. Mosteo
2004-10-13  0:12           ` Stephen Leake
2004-10-13  8:39             ` Pascal Obry
2004-10-13 13:11             ` Memory leak - What the ...? - FOUND Alex R. Mosteo
2004-10-17  0:45           ` Memory leak - What the ...? Brian May
2004-10-13  0:32         ` Matthew Heaney
2004-10-18  0:26           ` Brian May
2004-10-13  0:27       ` Matthew Heaney
2004-10-13  7:58         ` Martin Krischik
2004-10-13 13:01         ` Alex R. Mosteo
2004-10-13  0:25     ` Matthew Heaney
2004-10-13 12:26       ` Stephen Leake
2004-10-13 14:45         ` Matthew Heaney
2004-10-13 23:45           ` Brian May
2004-10-14  1:33         ` Jeffrey Carter
2004-10-11  8:04 ` Martin Dowie
2004-10-12 10:47   ` Alex R. Mosteo
2004-10-12 15:07 ` Alex R. Mosteo
2004-10-13 14:53   ` Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
2004-10-11  9:50 Christoph Karl Walter Grein
2004-10-11 10:21 Christoph Karl Walter Grein
2004-10-14  4:30 Christoph Karl Walter Grein

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