comp.lang.ada
 help / color / mirror / Atom feed
From: msimonides@power.com.pl
Subject: Access to interface conversion problem
Date: 13 Mar 2007 08:25:09 -0700
Date: 2007-03-13T08:25:09-07:00	[thread overview]
Message-ID: <1173799509.497206.119820@q40g2000cwq.googlegroups.com> (raw)

I have experienced problems upon using an access to tagged type object
converted to an access to ancestor type - Storage_Error is raised when
calling its operations.

After some investigation I found out that address of object (obtained
like this: Object_Ptr.all'Address) is different after conversion.
Using Unchecked_Deallocation for converting the access value solved
the problem.

The situation is analogous to that in program below:

with System.Address_Image; use System;
with Ada.Text_IO; use Ada.Text_IO;

procedure IFace_Cast_Bug is

   type Root_Type is interface;
   type Root_Access is access all Root_Type'Class;

   type Additional_Operations_Type is interface and Root_Type;
   type Additional_Operations_Access is access all
      Additional_Operations_Type'Class;

   type Root_Implementation_Type is new Root_Type with null record;
   type Root_Implementation_Access is access all
      Root_Implementation_Type'Class;

   type Final_Implementation_Type is new Root_Implementation_Type and
      Additional_Operations_Type with null record;
   type Final_Implementation_Access is access all
      Final_Implementation_Type'Class;


   function To_Additional_Operations (Object_Ptr :
Final_Implementation_Access)
      return Additional_Operations_Access
   is
   begin
      return Additional_Operations_Access (Object_Ptr);
   end To_Additional_Operations;


   Object_Ptr  : constant Final_Implementation_Access :=
      new Final_Implementation_Type;

   Iface_Ptr   : constant Additional_Operations_Access :=
      To_Additional_Operations (Object_Ptr);
begin
   Put      (Address_Image (Object_Ptr.all'Address));
   Put      (" /= ");
   Put_Line (Address_Image (IFace_Ptr.all'Address));
end IFace_Cast_Bug;

The output of this program is two addresses, the first one 4 less than
the second one (in my original program the difference is 12).
Removing the Root_Implementation_Type tagged type from inheritance
hierarchy results in identical addresses.

The question is:
Am I doing something wrong with the conversion? I believe that
upcasting should be perfectly safe and operations defined for the
target type should work on the access (and even incorrect downcast
should raise Constraint_Error instead of a Storage_Error postponed to
the first use).

To me this looks like a compiler bug.

I am using GNAT GPL 3.4.6 on Linux.
--
Marcin Simonides




             reply	other threads:[~2007-03-13 15:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-13 15:25 msimonides [this message]
2007-03-13 16:06 ` Access to interface conversion problem Robert A Duff
2007-03-13 16:54   ` Adam Beneschan
2007-03-13 17:35     ` Robert A Duff
2007-03-13 20:56 ` Simon Wright
2007-03-13 22:32   ` Adam Beneschan
2007-03-14  9:06     ` msimonides
replies disabled

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