comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: Question about OO programming in Ada
Date: Thu, 04 Dec 2003 16:32:17 -0500
Date: 2003-12-04T16:32:17-05:00	[thread overview]
Message-ID: <kb6dneVfAq1-OlKi4p2dnA@comcast.com> (raw)
In-Reply-To: <2prtsvgmt5lt3u1ulb5dvh8ba5nulfl3l3@4ax.com>

Dmitry A. Kazakov wrote:
> On Wed, 03 Dec 2003 20:23:53 -0500, "Robert I. Eachus"
> <rieachus@comcast.net> wrote:
> 
> 
>>Hyman Rosen wrote:
>>
>>
>>>Not on a pointer field within the object, but on the object itself.
>>
>>I'm very confused about why Hyman is confused.  Making a copy of an 
>>access value in Ada, and calling Free for both the original and the copy 
>>is bad juju.  Calling Free twice, or twenty times and passing the only 
>>the original access object is never a problem.
> 
> 
> It is worth to write a sample code to the problem:
> 
> with Ada.Finalization;...
 >
> The point is that calling Free from Finalize is revolting.

Yes, that is exactly the code you should never write.  If you have an 
object which is always designated by an access value, and you want the 
object to be deallocated "automagically," the Ada solution is to make 
the access variable a component of a record:

with Ada.Finalization;
package B is
    type Object_Ref is
       new Ada.Finalization.Limited_Controlled with private;
    record
       Got_It : Boolean := False; -- Prevents recursion
    end record;
    procedure Finalize (X : in out Object);

private

    type Object is limited...;
    type Object_Ptr is access Object'Class;
    type Object_Ref is
       new Ada.Finalization.Limited_Controlled with
    record
       Obj: Object_Ptr := new Object;
    end record;

end B;

with Ada.Unchecked_Deallocation;
with Text_IO;  use Text_IO;
package body B is

    procedure Free is
       new Ada.Unchecked_Deallocation (Object_Ref'Class, Object_Ptr);
    procedure Finalize (X : in out Object_Ref) is
    begin
       Free (X.Obj);
       Put_Line ("Deallocation");
    end Finalize;
end B;

with B; use B;
with Text_IO; use Text_IO;
procedure Test1 is
begin
    Put_Line ("Begin of the scope");
    declare
       X : Object_Ref; -- allocation done here.
    begin
      null;
    end; -- Finalize called here.
    Put_Line ("End of the scope");
end Test1;

There are lots of variations on this theme, including a version where 
Object_Ref is not limited and there is an Adjust, or where the allocator 
   is in an Initialize routine.  But this is the standard Ada 95 idiom 
for managing objects which must be on the stack.  The "pointer type" is 
a controlled record with an access type to the 'real' data on the heap.

But notice that the original version is a live hand grenade that can be 
set off in three ways.  If you create a Object on the stack, if an 
Object is not properly initialized, or if Finalize gets called more than 
once.   The 'clever' code to check Got_It is no real protection in the 
third case.  The storage designated by a junk pointer can have any value 
for Got_It.  In the second case, it may prevent deallocating an object 
that was never initialized, but that is a relatively minor problem 
(storage leak) compared to the other failure cases.

 >                                                     This would
 > not be necessary if finalization of the type Pointer called
 > deallocator in addition to a call to Finalize.

Think about this for a second, because there is a germ of a good idea 
here.  For access types for which assignment is valid, such a rule would 
be a disaster.  For example you don't want the next record in a linked 
list, and for that matter all of the rest of the list, deallocated if 
you remove an item from a list.  However, in my example above, the 
Object_Ref type is declared as limited.  What if we had true limited 
access types?

type Pointer is limited access Object;

For such a type deallocating the designated memory when the Pointer 
object goes away would make sense.  I just don't think it is a necessary 
language extension since it is so easy to create the abstraction when 
needed.  (And besides, there are many cases where you want the type to 
appear limited to users, but where you actually want to do assignment in 
the package body.)

-- 
                                           Robert I. Eachus

100% Ada, no bugs--the only way to create software.




  parent reply	other threads:[~2003-12-04 21:32 UTC|newest]

Thread overview: 109+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-25 19:04 Question about OO programming in Ada Ekkehard Morgenstern
2003-11-25 20:17 ` Randy Brukardt
2003-11-26  0:34   ` Ekkehard Morgenstern
2003-11-26  6:17     ` Vinzent 'Gadget' Hoefler
2003-11-26  9:29     ` Dmitry A. Kazakov
2003-11-26 15:54     ` Stephen Leake
2003-11-26 20:07       ` Randy Brukardt
2003-11-26 21:36         ` Stephen Leake
2003-11-26  8:56   ` Peter Hermann
2003-11-25 20:55 ` Martin Krischik
2003-11-26  0:22   ` Ekkehard Morgenstern
2003-11-26  1:00     ` Jeffrey Carter
2003-11-26 16:36     ` Martin Krischik
2003-11-26 18:09       ` Robert I. Eachus
2003-11-27 13:45         ` Jean-Pierre Rosen
2003-11-25 21:48 ` Stephen Leake
2003-11-26  0:01   ` Ekkehard Morgenstern
2003-11-26  1:16     ` Jeffrey Carter
2003-11-26 15:10     ` Georg Bauhaus
2003-11-26 15:48     ` Stephen Leake
2003-11-26 16:24       ` Hyman Rosen
2003-11-26 17:58     ` Robert I. Eachus
2003-11-27  2:10       ` Ekkehard Morgenstern
2003-11-27 10:15         ` Ludovic Brenta
2003-11-27 18:35         ` Jeffrey Carter
2003-11-28  4:35           ` Hyman Rosen
2003-11-28  7:28             ` Vinzent 'Gadget' Hoefler
2003-11-28  8:46               ` Dale Stanbrough
2003-11-28 10:16                 ` Vinzent 'Gadget' Hoefler
2003-12-01 15:57             ` Martin Krischik
2003-12-01 16:47               ` Hyman Rosen
2003-12-03 18:35                 ` Martin Krischik
2003-12-01 21:13               ` Jeffrey Carter
2003-12-02  8:47               ` Dmitry A. Kazakov
2003-12-03  9:29                 ` Pascal Obry
2003-12-03 11:26                   ` Dmitry A. Kazakov
2003-12-03 12:49                     ` Ludovic Brenta
2003-12-03 13:41                       ` Dmitry A. Kazakov
2003-12-03 14:11                         ` Ludovic Brenta
2003-12-03 14:45                           ` Dmitry A. Kazakov
2003-12-03 15:44                         ` Hyman Rosen
2003-12-03 16:11                           ` Dmitry A. Kazakov
2003-12-03 18:20                           ` David C. Hoos
     [not found]                           ` <28eb01c3b9ca$25b18870$b101a8c0@sy.com>
2003-12-03 18:35                             ` Hyman Rosen
2003-12-03 20:05                           ` Randy Brukardt
2003-12-03 20:57                             ` Hyman Rosen
2003-12-03 21:16                               ` Hyman Rosen
2003-12-03 22:04                           ` Pascal Obry
2003-12-03 22:34                             ` Hyman Rosen
2003-12-04  1:23                               ` Robert I. Eachus
2003-12-04  7:15                                 ` Hyman Rosen
2003-12-04 17:43                                   ` Warren W. Gay VE3WWG
2003-12-04  8:55                                 ` Dmitry A. Kazakov
2003-12-04 19:13                                   ` Randy Brukardt
2003-12-04 19:29                                     ` Hyman Rosen
2003-12-04 21:32                                   ` Robert I. Eachus [this message]
2003-12-05  8:43                                     ` Dmitry A. Kazakov
2003-11-27 22:12         ` Robert I. Eachus
2003-11-28  6:37           ` Simon Wright
2003-11-30  2:51             ` Robert I. Eachus
2003-12-06  7:48 ` Chad Bremmon
2003-12-06 13:33   ` Jeff C,
2003-12-06 22:44   ` Hyman Rosen
2003-12-07  3:02     ` Chad Bremmon
2003-12-07  7:53       ` Hyman Rosen
2003-12-07 15:34         ` James Rogers
2003-12-07 18:30           ` Martin Krischik
2003-12-07 20:25             ` James Rogers
2003-12-08  3:36               ` Hyman Rosen
2003-12-08  4:42                 ` Chad Bremmon
2003-12-08  8:42                   ` Hyman Rosen
2003-12-08  9:34                     ` Dmitry A. Kazakov
2003-12-08 13:25                       ` Hyman Rosen
2003-12-08 15:05                         ` Dmitry A. Kazakov
2003-12-09  4:38                           ` Hyman Rosen
2003-12-09  8:19                             ` Dmitry A. Kazakov
2003-12-09 13:29                               ` Hyman Rosen
2003-12-09 14:36                                 ` Dmitry A. Kazakov
2003-12-09 15:05                                   ` Hyman Rosen
2003-12-09 15:59                                     ` Dmitry A. Kazakov
2003-12-09 16:41                                       ` Hyman Rosen
2003-12-10 11:32                                         ` Dmitry A. Kazakov
2003-12-10 15:27                                           ` Hyman Rosen
2003-12-10 17:15                                             ` Dmitry A. Kazakov
2003-12-08 17:55                       ` Chad Bremmon
2003-12-08 23:09                         ` Hyman Rosen
2003-12-09  8:26                         ` Dmitry A. Kazakov
2003-12-08 19:33                       ` Martin Krischik
2003-12-09  4:41                         ` Hyman Rosen
2003-12-08 17:27                     ` Chad Bremmon
2003-12-08 18:44                       ` Georg Bauhaus
2003-12-08 19:27                         ` Martin Krischik
2003-12-08 19:36                         ` Chad Bremmon
2003-12-09  4:43                           ` Hyman Rosen
2003-12-08 23:23                       ` Hyman Rosen
2003-12-08 19:25                     ` Martin Krischik
2003-12-07 21:29           ` Peter C. Chapin
2003-12-08  3:44             ` Hyman Rosen
2003-12-08  3:46           ` Hyman Rosen
2003-12-08  5:54             ` James Rogers
2003-12-08  8:45               ` Hyman Rosen
2003-12-07 17:39         ` Chad Bremmon
2003-12-08 23:39           ` Hyman Rosen
2003-12-09  2:36             ` Chad Bremmon
2003-12-09  4:52               ` Hyman Rosen
2003-12-09 11:24               ` Georg Bauhaus
2003-12-09 18:42                 ` Chad Bremmon
2003-12-09 20:11                   ` Hyman Rosen
2003-12-08 23:40           ` Hyman Rosen
replies disabled

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