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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,65b902127ca8a604 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!cleanfeed1-b.proxad.net!nnrp14-2.free.fr!not-for-mail Date: Fri, 26 Jun 2009 23:03:30 +0200 From: Damien Carbonne User-Agent: Thunderbird 2.0.0.22 (X11/20090605) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Issue with GNAT GPL 2009 and GtkAda References: <4A414EBB.8060204@free.fr> <1avd65rn49abv$.krcxo2gdzb16$.dlg@40tude.net> <4a43c9ce$0$420$426a74cc@news.free.fr> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <4a4537a3$0$441$426a34cc@news.free.fr> Organization: Guest of ProXad - France NNTP-Posting-Date: 26 Jun 2009 23:03:31 MEST NNTP-Posting-Host: 82.247.219.63 X-Trace: 1246050211 news-4.free.fr 441 82.247.219.63:58581 X-Complaints-To: abuse@proxad.net Xref: g2news2.google.com comp.lang.ada:6650 Date: 2009-06-26T23:03:31+02:00 List-Id: Stephen Leake a �crit : > The problem is clear here. > > "D : in out Derived" is allowed to create a copy of the actual > parameter. Thus the 'Access in Foreach could be on a stack copy of D, > which will disappear eventually, leaving a dangling pointer in BB. So > the accessibility error is correct. > > If you replace "in out" in Process with "access": > > procedure Process (L : access Listener) is abstract; > > it works fine (with GNAT 6.2.1 on Windows). > > > Your original program had the same issue; I just missed it. > >> I find usage of 'Access quite annoying, because I never really know >> when there is an exception whether it is becauise my code is incorrect >> or because the compiler is incorrect. > > It is frustrating. > > It would be nice if the runtime error had some more info; in this case > "taking 'Access of a stack copy due to 'in out'" would be nice :). > > Part of the problem is the implicit conversion from object to access > in Process: > > procedure Process (D : in out Derived) is > begin > Ada.Text_IO.Put_Line ("Process"); > D.Foreach (2); > end Process; > > The call to Foreach is effectively: > > Foreach (D'access, 2); > > If you had written it that way, it would have been clearer what was > happening. > > The moral here: if the body of the program needs an access value, pass > in an access value! > > My code almost never uses 'Access, because it's better to create the > object via an allocator in the first place, and pass around access > values instead. > > Thanks for this puzzle; it has helped me realize exactly why I do > this! > >> The last simple example I wrote seems to indicate that GNAT has a >> bug, but I'm not an Ada expert ! If you also consider that there is >> a GNAT bug, I'll submit a bug to AdaCore. > > I now think this is not a compiler bug. > If what you say is true, why does the following example work fine (using GNAT), without any compiler warning or execution error ? As I told in a previous message, I don't want to change the "in out" mode of Interface (representing the code I wrote and that is independant of GtkAda or any graphical toolkit), because one day it could be used in cunjunction with third party code that uses access ! Otherwise, it would almost certainly mean that one needs to use access almost everywhere, even if it is meaningless. The following example works as I expect. If it should not, I have to do something (go back to school ?) ;-) ------------------------------------------------------------------- package Pack02 is type Base is tagged null record; procedure P1M (X : in out Base; Max_Depth : Positive); procedure P1A (X : access Base; Max_Depth : Positive); procedure P2M (X : in Base; Max_Depth : Positive); procedure P2A (X : access constant Base; Max_Depth : Positive); procedure P3M (X : out Base; Max_Depth : Positive); procedure P3A (X : access Base; Max_Depth : Positive); procedure Main; end Pack02; ------------------------------------------------------------------- with Ada.Text_IO; package body Pack02 is function Indent (X : Positive) return String is S : constant String (1 .. 6 - X) := (others => ' '); begin return S; end Indent; procedure P1M (X : in out Base; Max_Depth : Positive) is begin Ada.Text_IO.Put_Line (Indent (Max_Depth) & "P1M" & Positive'Image (Max_Depth)); if Max_Depth > 1 then P1A (X'Access, Max_Depth - 1); X.P1A (Max_Depth - 1); end if; end P1M; procedure P1A (X : access Base; Max_Depth : Positive) is begin Ada.Text_IO.Put_Line (Indent (Max_Depth) & "P1A" & Positive'Image (Max_Depth)); if Max_Depth > 1 then P1M (X.all, Max_Depth - 1); X.P1M (Max_Depth - 1); end if; end P1A; procedure P2M (X : in Base; Max_Depth : Positive) is begin Ada.Text_IO.Put_Line (Indent (Max_Depth) & "P2M" & Positive'Image (Max_Depth)); if Max_Depth > 1 then P2A (X'Access, Max_Depth - 1); X.P2A (Max_Depth - 1); end if; end P2M; procedure P2A (X : access constant Base; Max_Depth : Positive) is begin Ada.Text_IO.Put_Line (Indent (Max_Depth) & "P2A" & Positive'Image (Max_Depth)); if Max_Depth > 1 then P2M (X.all, Max_Depth - 1); X.P2M (Max_Depth - 1); end if; end P2A; procedure P3M (X : out Base; Max_Depth : Positive) is begin Ada.Text_IO.Put_Line (Indent (Max_Depth) & "P3M" & Positive'Image (Max_Depth)); if Max_Depth > 1 then P3A (X'Access, Max_Depth - 1); X.P3A (Max_Depth - 1); end if; end P3M; procedure P3A (X : access Base; Max_Depth : Positive) is begin Ada.Text_IO.Put_Line (Indent (Max_Depth) & "P3A" & Positive'Image (Max_Depth)); if Max_Depth > 1 then P3M (X.all, Max_Depth - 1); X.P3M (Max_Depth - 1); end if; end P3A; procedure main is X : aliased Base; begin P1M (X, 3); P2M (X, 3); P3M (X, 3); end main; end Pack02; ------------------------------------------------------------------- with Pack02; procedure Main02 is begin Pack02.Main; end Main02; -- Output: -- -- P1M 3 -- P1A 2 -- P1M 1 -- P1M 1 -- P1A 2 -- P1M 1 -- P1M 1 -- P2M 3 -- P2A 2 -- P2M 1 -- P2M 1 -- P2A 2 -- P2M 1 -- P2M 1 -- P3M 3 -- P3A 2 -- P3M 1 -- P3M 1 -- P3A 2 -- P3M 1 -- P3M 1 -------------------------------------------------------------------