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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,edd7ea1b2d7e9a18 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-02-13 12:55:37 PST Path: archiver1.google.com!news2.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!arclight.uoregon.edu!wn13feed!wn12feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!rwcrnsc53.POSTED!not-for-mail From: tmoran@acm.org Newsgroups: comp.lang.ada Subject: Re: Pitfall: freeing access discriminants References: X-Newsreader: Tom's custom newsreader Message-ID: NNTP-Posting-Host: 12.234.13.56 X-Complaints-To: abuse@attbi.com X-Trace: rwcrnsc53 1045169360 12.234.13.56 (Thu, 13 Feb 2003 20:49:20 GMT) NNTP-Posting-Date: Thu, 13 Feb 2003 20:49:20 GMT Organization: AT&T Broadband Date: Thu, 13 Feb 2003 20:49:20 GMT Xref: archiver1.google.com comp.lang.ada:34074 Date: 2003-02-13T20:49:20+00:00 List-Id: > type Integer_Access is access Integer; > type T(D: access Integer) is Ada.Limited_Controlled with null record; > > procedure Finalize(Object: in out T) is > -- Error: cannot convert access discriminant to non-local access type > D: Integer_Access := Integer_Access(Object.D); >... > What to do? It seems being a serious deficiency in Ada or there are a > workaround? Yet another case where "I can't easily shoot myself in the foot" is misinterpreted as "a serious deficiency in Ada". In my experience, if Ada tells you something is not legal, closer examination will usually show that it really wasn't a good design idea anyway. Since objects of type Integer_Access could be declared more globally than objects of type T, the Finalize routine might have included Global_Variable_Ptr := D; which would leave Global_Variable_Ptr pointing to unallocated memory. This error message shows that Ada is preventing that nasty error. Other messages have shown how you can get around it. You can still shoot in the direction of your foot, but you have to work to do it. > begin > Free(D); > end; In this case, you can shoot safely away from your foot with Christoph Grein's suggested: > procedure Finalize (Object: in out T) is > type Integer_Ptr is access all Integer; > procedure Free is new Ada.Unchecked_Deallocation (Integer, Integer_Ptr); > P: Integer_Ptr := Integer_Ptr (Object.D); > begin > Free (P); > end Finalize; I seems to me that you needn't worry that > Because of storage pools for Integer_Ptr may be different > from storage pool used by "new". because you get a standard storage pool unless you specify otherwise, so > type T(D: access Integer) > type Integer_Ptr is access all Integer; should both refer to the same storage pool.