comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: Freeing Pointers to classwide types
Date: 1998/10/09
Date: 1998-10-09T00:00:00+00:00	[thread overview]
Message-ID: <m3hfxex6sv.fsf@mheaney.ni.net> (raw)
In-Reply-To: 360d1380.165146@SantaClara01.news.InterNex.Net

tmoran@bix.com (Tom Moran) writes:

> > the normal coding would be to do a free operation in the
> >finalization routine
> I'm unable to see a way to code such a finalization routine:
> procedure Finalize(X : in out T) is
> begin
>    -- some code that frees the memory of X if it's on the heap 
>    -- and nulls out the (any) pointer to X
> end Finalize;

No.  You have to take the deallocation up a level, by wrapping the
allocation in a finalizable type.  A while back I posted an example like
this:

procedure Do_Someting (Stack : access Root_Stack'Class) is

   Handle : Iterator_Handle (Stack);

   Iterator : Stack_Iterator renames Get_Iterator (Handle).all;

begin
...

The type looks like

package Stacks is
...
   type Stack_Iterator is 
      abstract tagged limited null record;
   
   <primitive ops for Stack_Iterator>

   type Stack_Iterator_Access is 
      access all Stack_Iterator'Class;

   type Iterator_Handle (Stack : access Root_Stack'Class) is 
      limited private;

   function Get_Iterator 
     (Handle : Iterator_Handle) return Stack_Iterator_Access;

private

   function New_Iterator 
     (Stack : access Stack_Iterator) return Stack_Iterator_Access;

   type Iterator_Handle (...) is 
      new Limited_Controlled with record
         Iter : Stack_Iterator_Access := New_Iterator (Stack);
      end record;

   procedure Finalize (Handle : in out Iterator_Handle);

end;

package body Stacks is 

   procedure Finalize (Handle : in out Iterator_Handle) is
   begin
      <free Handle.Iter>
   end;

end;

                 
Never let a client call allocator new directly.  Let the allocation be
done by a controlled object, so that when the object is finalized, it
automatically reclaims the memory.







  parent reply	other threads:[~1998-10-09  0:00 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-09-24  0:00 Freeing Pointers to classwide types joecool
1998-09-25  0:00 ` alan walkington
1998-09-25  0:00 ` Tom Moran
1998-09-25  0:00   ` dewarr
1998-09-25  0:00     ` Tom Moran
1998-09-25  0:00       ` dewarr
1998-09-26  0:00     ` Tom Moran
1998-09-26  0:00       ` dewarr
1998-09-26  0:00         ` Tom Moran
1998-09-27  0:00           ` dewarr
1998-09-27  0:00             ` Tom Moran
1998-09-28  0:00               ` dewarr
1998-09-28  0:00                 ` Tom Moran
1998-09-28  0:00                   ` dewarr
1998-09-28  0:00                     ` Tom Moran
1998-09-28  0:00                       ` Tom Moran
1998-09-28  0:00                         ` Brian Rogoff
1998-09-28  0:00                       ` Pat Rogers
1998-09-28  0:00                         ` Tom Moran
1998-09-28  0:00                           ` Pat Rogers
1998-09-29  0:00                           ` dewarr
1998-09-29  0:00                             ` Tom Moran
1998-09-30  0:00                               ` Tom Moran
1998-10-01  0:00                                 ` dewar
1998-10-01  0:00                                   ` Tom Moran
1998-10-01  0:00                                     ` Samuel Tardieu
1998-10-01  0:00                                       ` Tom Moran
1998-10-01  0:00                                         ` Tucker Taft
1998-10-01  0:00                                           ` Tom Moran
1998-10-02  0:00                                           ` dewarr
1998-10-02  0:00                                             ` Larry Kilgallen
1998-10-02  0:00                                               ` dewarr
1998-10-02  0:00                                           ` dennison
1998-10-02  0:00                                             ` dewarr
1998-10-01  0:00                                         ` dennison
1998-10-02  0:00                                         ` dewarr
1998-10-09  0:00                                           ` Matthew Heaney
1998-10-09  0:00                                             ` dennison
1998-10-09  0:00                                               ` Matthew Heaney
1998-10-01  0:00                                     ` dewarr
1998-10-01  0:00                                       ` Tom Moran
1998-10-01  0:00                                     ` dewarr
1998-09-28  0:00                   ` dewarr
1998-09-28  0:00                     ` Richard D Riehle
1998-09-28  0:00                       ` Pat Rogers
1998-09-29  0:00                       ` dewarr
1998-10-09  0:00           ` Matthew Heaney [this message]
1998-09-25  0:00   ` Bob Fletcher
1998-09-25  0:00     ` dennison
1998-09-25  0:00     ` Samuel Tardieu
1998-10-09  0:00     ` Matthew Heaney
1998-10-12  0:00       ` Mats Weber
1998-10-12  0:00         ` Pat Rogers
1998-10-09  0:00   ` Matthew Heaney
1998-09-26  0:00 ` Simon Wright
1998-10-09  0:00 ` Matthew Heaney
1998-10-09  0:00   ` Niklas Holsti
1998-10-10  0:00     ` Matthew Heaney
1998-10-11  0:00       ` Niklas Holsti
1998-10-11  0:00         ` Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
1998-09-25  0:00 bpr5549
replies disabled

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