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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,46474020a5b291b7 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.megapath.net!news.megapath.net.POSTED!not-for-mail NNTP-Posting-Date: Thu, 21 Sep 2006 17:11:49 -0500 From: "Randy Brukardt" Newsgroups: comp.lang.ada References: <1158872883.994303.80430@b28g2000cwb.googlegroups.com> Subject: Re: Free'ing dynamic abstract tagged types.. Date: Thu, 21 Sep 2006 17:12:09 -0500 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1807 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 Message-ID: NNTP-Posting-Host: 64.32.209.38 X-Trace: sv3-LF3oX8QoiSOqJxcuCKSGqBIdGV3YJpfKdX6ent3jXqKpmo7doNso4N+yHgo5ciCgmtdfS+k/9DOgXNC!cKKo6E63Eb6htMJjqzzH5kaPK+VGa04b5XOd2vz6Zyo0fgndT/x3AKTzTRad2AXUGqvooPRgR8kX!pQpI2Re/6SdHVw== X-Complaints-To: abuse@megapath.net X-DMCA-Complaints-To: abuse@megapath.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news2.google.com comp.lang.ada:6691 Date: 2006-09-21T17:12:09-05:00 List-Id: "ldb" wrote in message news:1158872883.994303.80430@b28g2000cwb.googlegroups.com... > Ok, so I have this abstract tagged type called Person (for the sake of > discussion), and I have an Access_Person (which is access all > people'class). > > I have some derived types, like Man and Child, with their own added > fields. And I have defined Access_Man, Access_Child, as normal accesses > to these types. > > I want to free an Access_Person that was dynamically allocated. > ap : Access_Person; > begin > ap := Access_Person(new Man); > free(ap); > ... > > I can't figure out how to write the free routine, since unchecked > deallocation wants a pointer (presumably access_man), but all I have is > a class-wide pointer, and I can't figure out how, or if, I can convert > it. Does that make sense? No. ;-) You just free the access and let the compiler take care of it. That is, just instantiate Unchecked_Deallocation appropriately: procedure Person_Free is new Ada.Unchecked_Deallocation (Person'Class, Access_Person); and then call it. > Using free(ap.all), I can make an abstract function that will dispatch > and free any nested allocations, but I can't seem to get it to free the > record, itself. Right, you have to do that on the pointer. You could write something like: procedure Free (P : Access_Person) is begin Free (P.all); -- Free the contents Person_Free (P); -- Free the object itself. end Free; but if the derived types need internal clean-up, it's better to make the whole tree controlled and let the compiler do all of the internal clean-up: that makes it much harder to forget. Since you can't add Controlled to an inheritance tree after the fact, I think that *all* tagged type trees should be derived from Controlled or Limited_Controlled. (Otherwise, you're saying that the extensions don't need any clean-up, which is likely to be constraining.) Randy.