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: 399187824 Sender: matt@mheaney.ni.net References: <1ftmFTC69GA.191@samson.airnet.net> NNTP-Posting-Date: Thu, 08 Oct 1998 17:44:40 PDT Newsgroups: comp.lang.ada Date: 1998-10-09T00:00:00+00:00 List-Id: "joecool" writes: > I understand (I think) how to dynamically allocate different objects within > a class and place them in a heterogenous list but I'm not sure if I > understand how to properly free the memory. > > How do you go about freeing such critters? I like each specific type in the hierarchy to maintain its own free list of already-allocated objects. Provide a class-wide operation that dynamically dispatches a private Do_Free op. Something like: package P is type T is abstract null record; type T_Access is access all T'Class; for T_Access'Storage_Size use 0; -- -- By making this have a storage size of zero, you force -- clients to call a constructor (allocator) for the specific -- type. procedure Free (O : in out T_Access); ... private procedure Do_Free (O : access T); end; package body P is procedure Free (O : in out T_Access) is begin if O /= null then Do_Free (O); O := null; end if; end Free; procedure Do_Free (O : access T) is begin null; end; end P; Now create a derived type: package P.Q is type NT is new T with private; function New_NT (...) return T_Access; ... private type NT is ...; procedure Do_Free (O : access NT); end; package body P.Q is Free_List : ; function New_NT (...) return T_Access is begin if Free_List = null then allocate new NT object, and return that else pop item off free list, and return that end if; end; procedure Do_Free (O : access NT) is begin end; end P.Q; I worked up an example of this kind of thing for the Interpreter pattern. Email me if you like a copy of the code/text. Matt