comp.lang.ada
 help / color / mirror / Atom feed
From: NiGHTS <nights@unku.us>
Subject: Re: Strings with discriminated records
Date: Sun, 27 May 2018 20:05:53 -0700 (PDT)
Date: 2018-05-27T20:05:53-07:00	[thread overview]
Message-ID: <3c3b906f-a45d-4633-b0f3-55431c0f5ebb@googlegroups.com> (raw)
In-Reply-To: <9d001268-cba0-4eab-9830-6ec36e1a0d03@googlegroups.com>

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.


  reply	other threads:[~2018-05-28  3:05 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-26 21:43 Strings with discriminated records NiGHTS
2018-05-26 23:42 ` Shark8
2018-05-27  1:42   ` NiGHTS
2018-05-27  8:39     ` Dmitry A. Kazakov
2018-05-27 12:22       ` Mehdi Saada
2018-05-27 12:40         ` Dmitry A. Kazakov
2018-05-27 14:34       ` NiGHTS
2018-05-27 14:50         ` Dmitry A. Kazakov
2018-05-27 15:19           ` NiGHTS
2018-05-27 15:32             ` AdaMagica
2018-05-27 16:22               ` NiGHTS
2018-05-29 22:31           ` Randy Brukardt
2018-05-30  7:29             ` Dmitry A. Kazakov
2018-05-30 20:11               ` Randy Brukardt
2018-05-27 12:48     ` Mehdi Saada
2018-05-27 13:03       ` Dmitry A. Kazakov
2018-05-27 17:11 ` NiGHTS
2018-05-27 18:07   ` Simon Wright
2018-05-27 23:08     ` NiGHTS
2018-05-28  1:44       ` Jere
2018-05-28  3:05         ` NiGHTS [this message]
2018-05-28  3:23           ` NiGHTS
2018-05-27 18:25   ` Dmitry A. Kazakov
2018-05-27 22:44     ` NiGHTS
2018-05-28  7:29       ` Dmitry A. Kazakov
2018-05-28  7:42       ` Simon Wright
2018-05-28 18:38         ` Shark8
2018-05-28 21:15           ` Mehdi Saada
2018-05-28 21:48             ` Shark8
2018-05-28 22:27               ` Mehdi Saada
2018-05-28 23:59                 ` Shark8
2018-05-29  0:41                   ` Dan'l Miller
2018-05-30 17:11                     ` Shark8
2018-05-29  7:49                 ` Dmitry A. Kazakov
2018-05-29  9:31                   ` AdaMagica
2018-05-29 10:14                     ` Dmitry A. Kazakov
2018-05-29 13:40                   ` Dan'l Miller
2018-05-29 14:04                     ` Dmitry A. Kazakov
2018-05-29 22:41                       ` Randy Brukardt
2018-05-30  5:00                         ` J-P. Rosen
2018-05-30 20:09                           ` Randy Brukardt
2018-05-31  4:19                             ` J-P. Rosen
2018-05-31 22:18                               ` Randy Brukardt
2018-06-01 13:35                                 ` Dan'l Miller
2018-06-01 15:20                                   ` Dmitry A. Kazakov
2018-05-28 13:55 ` NiGHTS
2018-05-29 14:37 ` Mehdi Saada
2018-05-29 22:44   ` Randy Brukardt
2018-05-29 22:41 ` Mehdi Saada
2018-05-30 19:46   ` Randy Brukardt
2018-05-30 19:48     ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox