comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@acm.org>
To: comp.lang.ada@ada-france.org
Subject: Re: Memory leak - What the ...?
Date: 11 Oct 2004 14:24:10 -0400
Date: 2004-10-11T14:24:10-04:00	[thread overview]
Message-ID: <mailman.277.1097519068.390.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <dcb57d1e.0410110059.3064896@posting.google.com>

mosteo@gmail.com (Alex R. Mosteo) writes:

> Stephen Leake <stephen_leake@acm.org> wrote in message news:<mailman.270.1097458825.390.comp.lang.ada@ada-france.org>...
> > mosteo@gmail.com (Alex R. Mosteo) writes:
> > 
> > > 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.
> > 
> > Please post a complete compilable example, so I can run it with gnat 5.02a1.
> 
> Here's the example. Gnatmem shows that it leaks heavily. So I must
> have understood something really wrong about controlled types. I need
> to get this right.

Hmm. This inspired me to try GNAT.Debug_Pools. Here's the instrumented
code:

with Ada.Finalization;
with Ada.Streams;
with GNAT.Debug_Pools;
package Test_Aux is

   type Stream_Element_Array_Access is access Ada.Streams.Stream_Element_Array;
   Pool : GNAT.Debug_Pools.Debug_Pool;
   for Stream_Element_Array_Access'Storage_Pool use Pool;

   type Udp_Message is new Ada.Finalization.Controlled with record
      Data : Stream_Element_Array_Access;
   end record;

   function Create (Data : in Ada.Streams.Stream_Element_Array) return Udp_Message;

   procedure Adjust   (This : in out Udp_Message);
   procedure Finalize (This : in out Udp_Message);

end Test_Aux;
with Ada.Unchecked_Deallocation;
package body Test_Aux is

   function Create (Data : in Ada.Streams.Stream_Element_Array) return Udp_Message
   is
      Msg : Udp_Message :=
        (Ada.Finalization.Controlled with
           Data => new Ada.Streams.Stream_Element_Array'(Data));
   begin
      return Msg;
   end Create;

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

   procedure Finalize (This : in out Udp_Message) is
      procedure Free is new Ada.Unchecked_Deallocation
        (Ada.Streams.Stream_Element_Array, Stream_Element_Array_Access);
   begin
      Free (This.Data);
   end Finalize;

end Test_Aux;
with Ada.Exceptions;
with Ada.Streams; use Ada.Streams;
with Ada.Text_IO; use Ada.Text_IO;
with Test_Aux; use Test_Aux;
with GNAT.Debug_Pools;
procedure Test is
   procedure Pool_Info is new GNAT.Debug_Pools.Print_Info (Put_Line);

   Empty : Udp_Message;
   Arr   : array (1 .. 1000) of Udp_Message;
begin
   Put_Line ("Adding...");
   for I in Arr'Range loop
      Arr (I) := Create ((1 .. Stream_Element_Offset (I) => Stream_Element'First));
   end loop;

   Put_Line ("Deleting...");
   for I in Arr'Range loop
      Arr (I) := Empty;
   end loop;

   Pool_Info (Pool);
exception
when E: others =>
   Put_Line ("Exception: " & Ada.Exceptions.Exception_Information (E));
end test;

Compiling with GNAT 5.02a1 and running, we get:

gnatmake -k -g -O0 -gnatf -gnato -gnatwa -gnatwe -gnatwL -gnatVa -I..  test -largs   -bargs -E -cargs 
gcc -c -I./ -g -O0 -gnatf -gnato -gnatwa -gnatwe -gnatwL -gnatVa -I.. -I- ..\test.adb
gcc -c -I./ -g -O0 -gnatf -gnato -gnatwa -gnatwe -gnatwL -gnatVa -I.. -I- ..\test_aux.adb
gnatbind -aO./ -I.. -E -I- -x test.ali
gnatlink test.ali -g
./test.exe 
Adding...
Deleting...
Total allocated bytes :  1530000
Total logically deallocated bytes :  1530000
Total physically deallocated bytes :  0
Current Water Mark:  0
High Water Mark:  511008

Which seems to indicate no leak.

I don't see anything obviously wrong with the code.

There may be a bug with this in GNAT 3.15p on your OS.

Try adding Text_IO.Put_Line in each call, and trace one object's
lifetime; that often makes things clear.

-- 
-- Stephe




  reply	other threads:[~2004-10-11 18:24 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
replies disabled

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