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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!feeder.erje.net!newsfeed.velia.net!noris.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Unchecked_Deallocation of class-wide objects Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <9b4410c1-17a9-434c-b0e5-75843364ca36@r31g2000vbi.googlegroups.com> Date: Mon, 28 Sep 2009 11:12:04 +0200 Message-ID: NNTP-Posting-Date: 28 Sep 2009 11:12:05 CEST NNTP-Posting-Host: 1bd1c9b0.newsspool1.arcor-online.net X-Trace: DXC=DDTCa\W_kCFX36K@\WTHGJic==]BZ:afN4Fo<]lROoRA^YC2XCjHcbIhW8AT9f3U`IDNcfSJ;bb[EFCTGGVUmh?DLK[5LiR>kgB2=HeI[HQLo@ X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:8507 Date: 2009-09-28T11:12:05+02:00 List-Id: On Mon, 28 Sep 2009 01:43:03 -0700 (PDT), Maciej Sobczak wrote: > Is it legal and safe to deallocate class-wide objects? > > The problem is that such an object is allocated with its concrete > type, whereas deallocation is defined for its class-wide type. Yes, deallocation "dispatches" on the pointer's target. > Consider: > > type Shape is tagged private; > type Shape_Access is access Shape'Class; > > procedure Free_Shape is new Ada.Unchecked_Deallocation > (Object => Shape'Class, Name => Shape_Access); > -- ... > > type Circle is new Shape with ... > -- ... > C : Shape_Access := new Circle; > -- ... > Free_Shape (C); > > Is the Circle object allocated on the Shape-wide storage pool? From > what I understand, this is the condition for the above to work > properly. In Ada pool is bound to the access type, not to the target type, which is logical consequence that an object can be allocated on the stack. Another consequence is that it is meaningless to talk about Shape-wide-pool, however an implementation may indeed allocate objects of different types in different pools transparently to the program. If it chooses to do this for tagged types of the same hierarchy, then the pointer should become fat and contain the type tag in it. I know no Ada compiler that does it this way, but it is a possible scheme, IMO. > What if Circle is allocated for some Circle_Access type which is then > converted to Shape_Access? Can it be safely deallocated? You could not convert it because Shape_Access is pool-specific. (Unchecked_Conversion tells for itself) If it were a general access to class wide then deallocator would be "doubly dispatching" on the pool and on the target. Thus, as far as I can tell, it is safe in both cases. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de