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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ec2a500cce3658c4 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!proxad.net!freenix!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: Memory leak - What the ...? Date: 11 Oct 2004 14:24:10 -0400 Organization: Cuivre, Argent, Or Message-ID: References: NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: melchior.cuivre.fr.eu.org 1097519069 64554 212.85.156.195 (11 Oct 2004 18:24:29 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Mon, 11 Oct 2004 18:24:29 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: In-Reply-To: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Gateway to the comp.lang.ada Usenet newsgroup" List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: g2news1.google.com comp.lang.ada:5051 Date: 2004-10-11T14:24:10-04:00 mosteo@gmail.com (Alex R. Mosteo) writes: > Stephen Leake wrote in message news:... > > 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