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.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, FREEMAIL_REPLY autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,6b1a1ed8b075945 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!news.glorb.com!news-in.ntli.net!newsrout1-win.ntli.net!ntli.net!news.highwinds-media.com!xara.net!gxn.net!194.159.246.34.MISMATCH!peer-uk.news.demon.net!kibo.news.demon.net!news.demon.co.uk!demon!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Allocators and exceptions Date: Thu, 13 Sep 2007 00:43:47 +0100 Organization: Pushface Message-ID: References: <1189323618.588340.87180@o80g2000hse.googlegroups.com> <1189369871.672082.162750@50g2000hsm.googlegroups.com> <1189460936.295604.143720@r29g2000hsg.googlegroups.com> <1189502377.626510.172690@22g2000hsm.googlegroups.com> <1189601170.835400.72630@w3g2000hsg.googlegroups.com> NNTP-Posting-Host: pogner.demon.co.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.demon.co.uk 1189640628 3968 62.49.19.209 (12 Sep 2007 23:43:48 GMT) X-Complaints-To: abuse@demon.net NNTP-Posting-Date: Wed, 12 Sep 2007 23:43:48 +0000 (UTC) Cancel-Lock: sha1:TJnnp1cS6FrHzN8PMJbUv21AM6Y= User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (darwin) Xref: g2news2.google.com comp.lang.ada:1930 Date: 2007-09-13T00:43:47+01:00 List-Id: "Randy Brukardt" writes: > "Simon Wright" wrote in message > news:m2ir6fobaz.fsf@mac.com... >> Maciej Sobczak writes: >> >> > What should the constructor do *after* handling the exception? >> > Leave the object half-baked? >> >> I'm not sure exactly what Stephe meant by 'propagate'. It certainly >> seems a bad idea to allow a Constraint_Error to propagate >> unhandled. But what would be wrong with dealing with the problem and >> then raising an appropriate exception from the constructor? (even >> Constraint_Error if gnat makes sense). that > > Because Ada *requires* storage leakage in such cases (although some > compilers ignore the language definition and finalize anyway). An > allocated object cannot be finalized until it's *type* is finalized > or until an Unchecked_Deallocation is called -- and an > Unchecked_Deallocation is not going to be called if a constructor > propagates an exception. So the (inaccessible) object is supposed to > hang around for a long, long time. > > The "proper" way to handle this is to ensure that default initialize > of an object never propagates an exception, and then wrap the > allocator properly: > > type Access_T is access all T; > procedure Free is new Unchecked_Deallocation (T, Access_T); > function Alloc_Object (...) return Access_T is > A_T : Access_T := new T; -- Default initialized. > begin > A_T.all := ; > return A_T; > exception > when others => Free(A_T); return null; > end Alloc_Object; I guess I had misunderstood what's meant by 'constructor', becasue this is just what I had in mind ... and there are far worse things to leak than memory, such as locks, file handles, data structure integrity etc, and those we can handle even in a constructor (ie function returning a value of the type rather than a pointer to a new value of the type).