From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ed6a891101ff4e06 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: Freeing Pointers to classwide types Date: 1998/10/09 Message-ID: #1/1 X-Deja-AN: 399194928 Sender: matt@mheaney.ni.net References: <1ftmFTC69GA.191@samson.airnet.net> <360b26a1.41575272@SantaClara01.news.InterNex.Net> <6ugeu2$79u$1@nnrp1.dejanews.com> <360c4a70.29707515@SantaClara01.news.InterNex.Net> <6uifpt$e98$1@nnrp1.dejanews.com> <360d1380.165146@SantaClara01.news.InterNex.Net> NNTP-Posting-Date: Thu, 08 Oct 1998 18:05:01 PDT Newsgroups: comp.lang.ada Date: 1998-10-09T00:00:00+00:00 List-Id: 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; 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 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.