TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Classic View

Use Proportional Font
Show HTML Part by Default
Show All Mail Headers

Topic: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Mon, 20 Aug 2001 12:46:47 -0500
text/plain (47 lines)
One method (for more detail, look up Controlled types
in an Ada book, or see section 7.4 of the Ada 95 Rationale):


with Finalization;

........

   type A is access .....;

   type Needs_Guaranteed_Heap_Cleanup is
      new Finalization.Controlled with
      record
         Pointer : A;
      end record;

....

   procedure Finalize (Item : Needs_Guaranteed_Heap_Cleanup) is
   -- automatically called when Item exits scope.
   -- client of Item cannot prevent it (or forget it).
   begin
      Free (Item.Pointer);
      Finalize (Controlled(Item));
   end Finalize;

   type File_That_Needs_Guaranteed_Closing is
      new Finalization.Controlled with
      record
         File : XXX_IO.File_Type;
      end record;

....

   procedure Finalize (Item : File_That_Needs_Guaranteed_Closing) is
   -- automatically called when Item exits scope.
   -- client of Item cannot prevent it (or forget it).
   begin
      if XXX_IO.Is_Open (Item.File) then
         XXX_IO.Close (Item.File);
      end if;
      Finalize (Controlled(Item));
   end Finalize;

Another method is described in an article on the Web.  URI recently posted
in comp.lang.ada but I don't remember it.

ATOM RSS1 RSS2