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=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:a24:94ce:: with SMTP id j197-v6mr5662647ite.37.1527476753725; Sun, 27 May 2018 20:05:53 -0700 (PDT) X-Received: by 2002:a54:4381:: with SMTP id u1-v6mr78584oiv.10.1527476753483; Sun, 27 May 2018 20:05:53 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!news.swapon.de!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!v8-v6no4846656itc.0!news-out.google.com!f20-v6ni4660itd.0!nntp.google.com!v8-v6no4846654itc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 27 May 2018 20:05:53 -0700 (PDT) In-Reply-To: <9d001268-cba0-4eab-9830-6ec36e1a0d03@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=73.205.150.94; posting-account=Ru7E4QoAAAC_HiQ2D8LjZ7rh1mbTNcVn NNTP-Posting-Host: 73.205.150.94 References: <55ce14eb-6b83-4ea0-a550-f9e1410d0b06@googlegroups.com> <51dfb377-1b3e-45ca-a211-158101efe557@googlegroups.com> <9d001268-cba0-4eab-9830-6ec36e1a0d03@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <3c3b906f-a45d-4633-b0f3-55431c0f5ebb@googlegroups.com> Subject: Re: Strings with discriminated records From: NiGHTS Injection-Date: Mon, 28 May 2018 03:05:53 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader02.eternal-september.org comp.lang.ada:52745 Date: 2018-05-27T20:05:53-07:00 List-Id: On Sunday, May 27, 2018 at 9:44:22 PM UTC-4, Jere wrote: > On Sunday, May 27, 2018 at 7:08:03 PM UTC-4, NiGHTS wrote: > > On Sunday, May 27, 2018 at 2:07:37 PM UTC-4, Simon Wright wrote: > > > > > > > I've expanded my example to include a strange run-time crash I am getting. > > > > > > > > Type Message (Length : Positive) is new Ada.Finalization.Controlled with record > > > > Text : String (1 .. Length); > > > > Cstr : Interfaces.C.Strings.chars_ptr; > > > > end record; > > > > > > > > function Create (Value : String) return Message is > > > > begin > > > > return ( > > > > Ada.Finalization.Controlled with Length => Value'Length, > > > > Text => Value, > > > > Cstr => Interfaces.C.Strings.New_String (Value) > > > > ); > > > > end Create; > > > > > > > > procedure Finalize ( > > > > M : in out Message > > > > ) is > > > > begin > > > > Interfaces.C.Strings.Free ( M.Cstr ); -- Crashes here > > > > end; > > > > > > > > declare > > > > M : Message := Create ("Hello World"); > > > > begin > > > > null; > > > > end; > > > > > > > > Why does my program crash on Finalization? > > > > > > Maybe Finalize is getting called twice? You should set M.Cstr to > > > Null_Ptr after freeing it. > > > > It doesn't work. On each automatic call of Finalize, the object data is reset. Its very strange. > > It won't work unless you have an appropriate Adjust. Since your object is > controlled (and not limited controlled), the compiler is making temporaries, > each with their own copy of the pointer (someone else mentioned this above). > Each temporary will be finalized and each will have their own copy of > the pointer, so nulling it only nulls that objects copy and not the others. > > As stated above by someone else, you'll either have to do a clone with > separately allocated memory or some sort of reference count that only > allows finalize to call free when the last copy calls finalize (and > ignores the others calls). > > Dmitry has an example of how to do this with an intrusive reference > count with his handles/objects library (see his website). > > Making the type limited controlled is also a solution since it cannot > make temporaries for one like it can a regular controlled object. Yes, I understand now. I will do some tests and see what works for me. Thank you.