comp.lang.ada
 help / color / mirror / Atom feed
From: stefan-lucks@see-the.signature
Subject: Re: unexpected behaviour of finalize
Date: Fri, 4 Apr 2008 19:51:28 +0200
Date: 2008-04-04T19:51:28+02:00	[thread overview]
Message-ID: <Pine.LNX.4.64.0804041944350.3841@medsec1.medien.uni-weimar.de> (raw)
In-Reply-To: <308722a0-7372-4a2b-9dbd-8d8704a3fcae@s33g2000pri.googlegroups.com>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 2140 bytes --]

> This looks like a pretty clear compiler bug, especially since by the
> time Finalize is called by the outermost Do_Something, it's using, for
> the value of I, the parameter from a Do_Something call that is no
> longer active.  It shouldn't be hard to modify this slightly, using
> (say) a string or record as a parameter to Do_Something, to create a
> test where utter garbage is displayed; then it would be clearer that
> this is a compiler bug.

Good idea! Yes, the following change makes the program output a bit of 
garbage, as you guessed:

procedure Strong_Fin_Test_Case is

   type Fin is new Ada.Finalization.Limited_Controlled with null record;

   procedure Do_Something (I: Natural) is

      A: String(1 .. 5); -- this is new

      type My_Fin is new Fin with null record;
      overriding procedure Finalize(Self: in out My_Fin) is
      begin
         Ada.Text_IO.Put_Line("  Do_Something Finalization " & A);
      end Finalize;
      Singleton: My_Fin;

   begin -- Do_Something
      Ada.Text_IO.Put_Line("  I = " & Natural'Image(I));
      if I>0 then
         Do_Something(I-1);
         Do_Something(I-1);
      end if;
      A(1 .. 5) := ("ABCDE"); -- always the same
   end Do_Something;

begin
   Ada.Text_IO.Put_Line("Start Do_Something (2)");
   Do_Something(2);
   Ada.Text_IO.Put_Line("Stop Do_Something (2)");
end Strong_Fin_Test_Case;


The putput:


Start Do_Something (2)
  I =  2
  I =  1
  I =  0
  Do_Something Finalization ABCDE
  I =  0
  Do_Something Finalization ABCDE
  Do_Something Finalization ABCDE
  I =  1
  I =  0
  Do_Something Finalization ABCDE
  I =  0
  Do_Something Finalization ABCDE
  Do_Something Finalization ABCDE
  Do_Something Finalization �l
Stop Do_Something (2)

The last line before "Stop Do_Something (2)" contains some strange 
characters instead of the constant "ABCDE" it should output. Furthermore, 
this output is not always the same. 

So long

Stefan



-- 
------ Stefan Lucks   --  Bauhaus-University Weimar  --   Germany  ------
               Stefan dot Lucks at uni minus weimar dot de
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------

      parent reply	other threads:[~2008-04-04 17:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-04  9:33 unexpected behaviour of finalize stefan-lucks
2008-04-04 16:08 ` Adam Beneschan
2008-04-04 17:34   ` george.priv
2008-04-04 17:38   ` george.priv
2008-04-04 17:52     ` stefan-lucks
2008-04-04 19:53       ` george.priv
2008-04-04 17:51   ` stefan-lucks [this message]
replies disabled

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