comp.lang.ada
 help / color / mirror / Atom feed
From: "Hibou57 (Yannick Duchêne)" <yannick_duchene@yahoo.fr>
Subject: Protected object which never get finalized
Date: Sat, 13 Feb 2010 01:45:40 +0100
Date: 2010-02-13T01:45:40+01:00	[thread overview]
Message-ID: <op.u71i2edlvwnd5a@garhos> (raw)


Hi,

I may be not lucky : after the case of an object of a protected type being  
finalized too much soon, here is now that I have to do with an object of a  
protected type which never gets finalized (think I was to use this latter  
as a work around after long try failure with the former, I'm really not  
lucky at all).

Inquisitive peoples with interests in the case, may have a look at the  
following test : two protected types are defined, with exact equivalent  
definition. One is defined in a package, the other is defined in the local  
scope. An instance of the first (the one defined  in the package), never  
get finalized. An instance of the second type (declared in the local  
scope), goes through its normal life.

But perhaps there's something I did not understood


with Ada.Text_IO;
with Ada.Finalization;

procedure Test is
    -- This test program exposes a protected
    -- object which is initialized, but never
    -- get finalized (object of type A_Type).

    -- Don't care starting here if you just want to
    -- have a quick look, jump to the comment
    -- "Important things starts here" later below.

    package Spies is
       type Instance_Type (Client_Name : access String) is
          limited private;
    private
       type Instance_Type (Client_Name : access String) is
          new Ada.Finalization.Limited_Controlled
          with null record;
       overriding procedure Initialize
         (Instance : in out Instance_Type);
       overriding procedure Finalize
         (Instance : in out Instance_Type);
    end Spies;

    package body Spies is
       overriding procedure Initialize
         (Instance : in out Instance_Type)
       is
          Me : Instance_Type renames Instance;
       begin
          Ada.Text_IO.Put_Line
            ("Spy : I am coping with Initialization of " &
             Me.Client_Name.all &
             ".");
       end;
       overriding procedure Finalize
         (Instance : in out Instance_Type)
       is
          Me : Instance_Type renames Instance;
       begin
          Ada.Text_IO.Put_Line
            ("Spy : I am coping with Finalization of " &
             Me.Client_Name.all &
             ".");
       end;
    end Spies;

    -- Important things starts here

    package P is
       type A_Type is limited private;
    private
       protected type A_Type is
       private
          Spy : Spies.Instance_Type
            (Client_Name => new String'("P.A_Type"));
       end;
    end P;

    package body P is

       protected body A_Type is
       end;

    end P;

    protected type B_Type is
    private
       Spy : Spies.Instance_Type
         (Client_Name => new String'("B_Type"));
    end;

    protected body B_Type is
    end;

begin
    declare
       Tested_Object : P.A_Type;
    begin
       Ada.Text_IO.Put_Line ("Inside block testing A_Type");
       -- Tested_Object is initialized, but never
       -- get finalized, while [ARM 7.6.1(8)] requires
       -- finalization for protected types. Or am I wrong ?
    end;

    Ada.Text_IO.New_Line;

    declare
       Tested_Object : B_Type;
    begin
       Ada.Text_IO.Put_Line ("Inside block testing B_Type");
       -- This one, with a strictly equivalent declaration, which
       -- is just not declared inside a package, get finalized
       -- as expected.
    end;
end Test;



             reply	other threads:[~2010-02-13  0:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-13  0:45 Hibou57 (Yannick Duchêne) [this message]
2010-02-14 12:35 ` Protected object which never get finalized John B. Matthews
2010-02-15 18:08   ` sjw
2010-02-15 19:22     ` Hibou57 (Yannick Duchêne)
2010-02-15 19:20   ` Hibou57 (Yannick Duchêne)
2010-02-15 20:23     ` John B. Matthews
2010-02-15 20:44       ` Adam Beneschan
2010-02-15 22:26       ` Hibou57 (Yannick Duchêne)
2010-02-16  0:23         ` John B. Matthews
replies disabled

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