comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Strings with discriminated records
Date: Mon, 28 May 2018 09:29:06 +0200
Date: 2018-05-28T09:29:06+02:00	[thread overview]
Message-ID: <pegb42$1lqn$1@gioia.aioe.org> (raw)
In-Reply-To: 090d6eb2-9f52-4471-a22e-ce1bdf457188@googlegroups.com

On 2018-05-28 00:44, NiGHTS wrote:

> I confirmed with an Ada.Text_IO.Put_Line() that Finalize was called three times.

It is called for three different objects.

> I then disabled Finalize and created an explicitly called Finalize2 function and it worked fine. It's weird though because my program only elaborated the object once, so why did it finalize three times?

Because you have three copies of the controlled object. Here is a fixed 
implementation with Adjust:
-------------------------------------------------------
with Ada.Text_IO;          use Ada.Text_IO;
with Interfaces.C;         use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;

with Ada.Finalization;

procedure Test is

    package P is
       type C_String is new Ada.Finalization.Controlled with private;
       function Create (Text : String) return C_String;
       overriding procedure Adjust (Text : in out C_String);
       overriding procedure Finalize (Text : in out C_String);
    private
       type C_String is new Ada.Finalization.Controlled with record
          Ptr : chars_ptr;
       end record;
    end P;

    package body P is
       function Create (Text : String) return C_String is
       begin
          Put_Line ("Creating:" & Text);
          return (Ada.Finalization.Controlled with New_String (Text));
       end Create;

       procedure Adjust (Text : in out C_String) is
       begin
          if Text.Ptr = Null_Ptr then
             Put_Line ("Copying null string:");
          else
             Put_Line ("Copying:" & Value (Text.Ptr));
             Text.Ptr := New_String (Value (Text.Ptr));
          end if;
       end Adjust;

       procedure Finalize (Text : in out C_String) is
       begin
          if Text.Ptr = Null_Ptr then
             Put_Line ("Finalizing null string:");
          else
             Put_Line ("Finalizing:" & Value (Text.Ptr));
             Free (Text.Ptr);
          end if;
       end Finalize;
    end P;

    use P;
    S1 : C_String := Create ("Hello");
begin
    null;
end Test;
-------------------------------------------------
It prints:

Creating:Hello
Copying:Hello
Finalizing:Hello
Copying:Hello
Finalizing:Hello
Finalizing:Hello

Now the meaning of this:

Creating:Hello   -- Create
Copying:Hello    -- Copy local object (aggregate?) in Create
Finalizing:Hello -- Finalize the local object in Create
Copying:Hello    -- Copy result of Create to S1
Finalizing:Hello -- Finalize the result of Create
Finalizing:Hello -- Finalize S1

> I tried wrapping the body of finalize in a condition to force it to run only once, yet it still managed to run it again ignoring the boolean. I'm perplexed. Not sure if I should ever trust Ada.Finalization.Controlled.

Yes, see the example.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  reply	other threads:[~2018-05-28  7:29 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
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 [this message]
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