comp.lang.ada
 help / color / mirror / Atom feed
From: fedya_fedyakoff@inbox.ru
Subject: another way to shoot yourself in the foot?
Date: Fri, 20 Jun 2008 02:03:32 -0700 (PDT)
Date: 2008-06-20T02:03:32-07:00	[thread overview]
Message-ID: <54157920-377a-441b-9b0b-f0c4f9ddffec@f36g2000hsa.googlegroups.com> (raw)

Consider a very simple code. Suppose we have

1) a Named interface as this

--- named.ads ---

package Named is
   type Object is limited interface;

   function  name(this: Object) return String is abstract;
end Named;

2) Simple implementation

--- Some.ads ---

with Named;
with Ada.Finalization; use Ada.Finalization;
with Ada.Strings.Bounded;

package Some is

   Size : constant := 80;

   package bs is new Ada.Strings.Bounded.Generic_Bounded_Length(Size);
   use bs;

   type Object is new Limited_Controlled and Named.Object with
private;


   function Create( name: String ) return Object;

   overriding procedure Initialize(this: in out Object);
   overriding procedure Finalize(this: in out Object);

   overriding function  name(this: Object) return String;


private
   type Object is new Limited_Controlled and Named.Object with
      record
         the_name: Bounded_String;
      end record;
end Some;

--- Some.adb ---

with ada.Text_IO;

package body Some is
   package tio renames ada.Text_IO;

   function Create( name: String ) return Object is
   begin
      return O : Object do
         O.the_name := To_Bounded_String(name);
      end return;
   end Create;


   procedure Initialize(this: in out Object) is
   begin
      tio.Put_Line("Initialization Car " & this.name);
   end Initialize;

   procedure Finalize(this: in out Object) is
   begin
      tio.Put_Line("Finalization Car " & this.name);
   end Finalize;


   function name(this: Object) return String is
   begin
      return To_String(this.the_name);
   end name;

end Some;

3) Simple factory function as follows:

--- Factory ads ---
with Named;
with Some;

package Factory is
   function Create(class: String; name: String) return
Named.Object'Class;
end Factory;

--- Factory.adb ---

with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Some;

package body Factory is
   --- this one is ugly, but in sake of simplicity ...
   function Create(class: String; name: String) return
Named.Object'Class is
   begin
      if Index(class, "Some") /= 0 then
         return Some.Create(name);
      end if;
      raise Program_Error with "Unknown class " & class;
   end;
end Factory;
---------------

4) And finally main procedure

--- boom.adb ---

with ada.Text_IO;
with Named; use Named;
with Factory;
with Some;

procedure boom is
   package tio renames ada.Text_IO;
   aNamed: Some.Object := Some.Create("One");
begin
     declare
        aNamed1: Named.Object'Class := Factory.Create("Some", "Two");
     begin
        tio.put_line( "car: " & aNamed1.name ); ---booooooommm!!!
     end;

   tio.put_line( "car: " & aNamed.name );
   tio.Flush;
end boom;
-----------


All of that being compiled without even one warning, crashed at
runtime with PROGRAM_ERROR EXCEPTION_ACCESS_VIOLATION  on Windows
( compiled using gnat gpl 2008 )

Modified program  ( Named.Object, being changed to be not limited, and
Some.Object to be Controlled ) runs perfectly.

I have an idea why the program behavs like that, but i think there
must be some warning at least.

What do you think?




             reply	other threads:[~2008-06-20  9:03 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-20  9:03 fedya_fedyakoff [this message]
2008-06-20  9:34 ` another way to shoot yourself in the foot? Dmitry A. Kazakov
2008-06-20  9:48   ` fedya_fedyakoff
2008-06-20 10:01     ` Ludovic Brenta
2008-06-20 10:05 ` christoph.grein
2008-06-20 10:26   ` Dmitry A. Kazakov
2008-06-20 16:12     ` Adam Beneschan
2008-06-20 15:48   ` Adam Beneschan
2008-06-20 19:27   ` Robert A Duff
2008-06-20 23:37     ` Jeffrey R. Carter
2008-06-21  8:56       ` Dmitry A. Kazakov
2008-06-22 20:44         ` Robert A Duff
2008-06-23  7:49           ` Dmitry A. Kazakov
2008-06-24  4:02             ` george.priv
2008-06-24  7:30               ` Dmitry A. Kazakov
2008-06-24 17:16                 ` Robert A Duff
2008-06-24 19:15                   ` Jeffrey R. Carter
2008-06-24 20:31                     ` Robert A Duff
2008-06-24 20:50                       ` Ludovic Brenta
2008-06-24 23:02                         ` Robert A Duff
2008-06-24 23:42                         ` Georg Bauhaus
2008-06-24 21:24                       ` Jeffrey R. Carter
2008-06-24 23:24                         ` Robert A Duff
2008-06-25 15:07                       ` Adam Beneschan
2008-06-24 14:59             ` Adam Beneschan
2008-06-24 16:41               ` Dmitry A. Kazakov
2008-06-24 17:20                 ` Robert A Duff
2008-06-24 17:52                   ` Dmitry A. Kazakov
2008-06-24 23:35                     ` Georg Bauhaus
2008-06-25  8:09                       ` Dmitry A. Kazakov
2008-06-25 10:32                         ` Georg Bauhaus
2008-06-25 12:06                           ` Dmitry A. Kazakov
2008-06-22 20:37       ` Robert A Duff
2008-06-22 21:25         ` Jeffrey R. Carter
2008-07-04 20:52           ` Colin Paul Gloster
2008-07-04 22:15             ` (see below)
2008-07-05 16:06               ` Colin Paul Gloster
2008-07-05 13:38             ` Gary Scott
2008-07-05 16:42               ` Colin Paul Gloster
2008-07-05 19:00                 ` Gary Scott
2008-07-09 19:39                   ` Colin Paul Gloster
2008-07-09 20:35                     ` Richard Maine
2008-07-09 22:49                       ` Terence
2008-07-10  1:07                         ` Gary Scott
2008-07-10 14:10                       ` Colin Paul Gloster
2008-07-10 14:57                         ` fj
2008-07-10 16:47                           ` Richard Maine
2008-07-10 17:03                         ` Dick Hendrickson
2008-07-10 17:26                           ` Craig Powers
2008-07-10 19:55                             ` James Giles
2008-07-10 20:45                               ` Dick Hendrickson
2008-07-10 21:22                                 ` Richard Maine
2008-07-10 21:29                                   ` Craig Powers
2008-07-10 20:45                               ` Craig Powers
2008-07-10 19:51                           ` James Giles
2008-07-11 15:02                             ` Colin Paul Gloster
replies disabled

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