comp.lang.ada
 help / color / mirror / Atom feed
* Protected object which never get finalized
@ 2010-02-13  0:45 Hibou57 (Yannick Duchêne)
  2010-02-14 12:35 ` John B. Matthews
  0 siblings, 1 reply; 9+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2010-02-13  0:45 UTC (permalink / 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;



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Protected object which never get finalized
  2010-02-13  0:45 Protected object which never get finalized Hibou57 (Yannick Duchêne)
@ 2010-02-14 12:35 ` John B. Matthews
  2010-02-15 18:08   ` sjw
  2010-02-15 19:20   ` Hibou57 (Yannick Duchêne)
  0 siblings, 2 replies; 9+ messages in thread
From: John B. Matthews @ 2010-02-14 12:35 UTC (permalink / raw)


In article <op.u71i2edlvwnd5a@garhos>,
 Hibou57 (Yannick Duchêne) <yannick_duchene@yahoo.fr> wrote:

> 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

I get the same result with gcc 4.3.4, but it works if protected type 
A_Type is public in P:

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

Type A_Type has its own private part, and this seems more comparable to 
the declaration of protected type B_Type. Is there any reason to make 
A_Type more opaque?

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Protected object which never get finalized
  2010-02-14 12:35 ` 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)
  1 sibling, 1 reply; 9+ messages in thread
From: sjw @ 2010-02-15 18:08 UTC (permalink / raw)


On Feb 14, 12:35 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:

> I get the same result with gcc 4.3.4, but it works if protected type
> A_Type is public in P:
>
>    package P is
>       protected type A_Type is
>       private
>           Spy : Spies.Instance_Type
>             (Client_Name => new String'("P.A_Type"));
>       end;
>    end P;
>
> Type A_Type has its own private part, and this seems more comparable to
> the declaration of protected type B_Type. Is there any reason to make
> A_Type more opaque?

I had wanted to write

    private
       Name : aliased constant String := "P.A_Type";
       protected type A_Type is
          private
             Spy : Spies.Instance_Type
               (Client_Name => Name'Access);
       end;
    end P;

to avoid allocating a new Client_Name at each instance creation. This
looks a little messy if in the public part.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Protected object which never get finalized
  2010-02-14 12:35 ` John B. Matthews
  2010-02-15 18:08   ` sjw
@ 2010-02-15 19:20   ` Hibou57 (Yannick Duchêne)
  2010-02-15 20:23     ` John B. Matthews
  1 sibling, 1 reply; 9+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2010-02-15 19:20 UTC (permalink / raw)


Le Sun, 14 Feb 2010 13:35:47 +0100, John B. Matthews  
<nospam@nospam.invalid> a écrit:
> Type A_Type has its own private part, and this seems more comparable to
> the declaration of protected type B_Type. Is there any reason to make
> A_Type more opaque?
>
Well, let say first I don't like the idea of designing with a compiler bug  
is mind, and secondly, this is not to be part of a public part (I don't  
really like private stuff in public part).
When I know a mostly clean workaround, that's Ok, but I do not like this  
one.
TBH, as was trying (as explained in the initial post), to workaround  
another bug when I meet this one.
The interesting point to me, is that you get the same, so it is not just  
my compiler. This means for me I should report the bug to AdaCore (as you  
seem to have confirmed the bug).

I thank you for the test you've made :)

-- 
No-no, this isn't an oops ...or I hope (TM) - Don't blame me... I'm just  
not lucky



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Protected object which never get finalized
  2010-02-15 18:08   ` sjw
@ 2010-02-15 19:22     ` Hibou57 (Yannick Duchêne)
  0 siblings, 0 replies; 9+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2010-02-15 19:22 UTC (permalink / raw)


Le Mon, 15 Feb 2010 19:08:18 +0100, sjw <simon.j.wright@mac.com> a écrit:
> I had wanted to write
>
>     private
>        Name : aliased constant String := "P.A_Type";
>        protected type A_Type is
>           private
>              Spy : Spies.Instance_Type
>                (Client_Name => Name'Access);
>        end;
>     end P;
>
> to avoid allocating a new Client_Name at each instance creation.
Don't bother, this String is just required for the Spy member, which was  
just there to test how things was working. This was not to be part of the  
final application.

> This looks a little messy if in the public part.
Yes, ;)


-- 
No-no, this isn't an oops ...or I hope (TM) - Don't blame me... I'm just  
not lucky



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Protected object which never get finalized
  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)
  0 siblings, 2 replies; 9+ messages in thread
From: John B. Matthews @ 2010-02-15 20:23 UTC (permalink / raw)


In article <op.u76nzoymvwnd5a@garhos>,
 Hibou57 (Yannick Duchêne) <yannick_duchene@yahoo.fr> wrote:

> Le Sun, 14 Feb 2010 13:35:47 +0100, John B. Matthews  
> <nospam@nospam.invalid> a écrit:
> > Type A_Type has its own private part, and this seems more comparable to
> > the declaration of protected type B_Type. Is there any reason to make
> > A_Type more opaque?
> >
> Well, let say first I don't like the idea of designing with a 
> compiler bug  is mind, and secondly, this is not to be part of a 
> public part (I don't  really like private stuff in public part). When 
> I know a mostly clean workaround, that's Ok, but I do not like this  
> one.
> TBH, as was trying (as explained in the initial post), to workaround  
> another bug when I meet this one. The interesting point to me, is 
> that you get the same, so it is not just  my compiler. This means for 
> me I should report the bug to AdaCore (as you  seem to have confirmed 
> the bug).

I wish I could claim to understand it well enough to say one way or the 
other. Given the desired declaration,

    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; 

is "protected type A_Type" a valid completion of "type A_Type is limited 
private;" or is that irrelevant?

> I thank you for the test you've made :)

You're welcome; thank you for an interesting example.

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Protected object which never get finalized
  2010-02-15 20:23     ` John B. Matthews
@ 2010-02-15 20:44       ` Adam Beneschan
  2010-02-15 22:26       ` Hibou57 (Yannick Duchêne)
  1 sibling, 0 replies; 9+ messages in thread
From: Adam Beneschan @ 2010-02-15 20:44 UTC (permalink / raw)


On Feb 15, 12:23 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article <op.u76nzoymvwnd5a@garhos>,
>  Hibou57 (Yannick Duchêne) <yannick_duch...@yahoo.fr> wrote:
>
>
>
>
>
> > Le Sun, 14 Feb 2010 13:35:47 +0100, John B. Matthews  
> > <nos...@nospam.invalid> a écrit:
> > > Type A_Type has its own private part, and this seems more comparable to
> > > the declaration of protected type B_Type. Is there any reason to make
> > > A_Type more opaque?
>
> > Well, let say first I don't like the idea of designing with a
> > compiler bug  is mind, and secondly, this is not to be part of a
> > public part (I don't  really like private stuff in public part). When
> > I know a mostly clean workaround, that's Ok, but I do not like this  
> > one.
> > TBH, as was trying (as explained in the initial post), to workaround  
> > another bug when I meet this one. The interesting point to me, is
> > that you get the same, so it is not just  my compiler. This means for
> > me I should report the bug to AdaCore (as you  seem to have confirmed
> > the bug).
>
> I wish I could claim to understand it well enough to say one way or the
> other. Given the desired declaration,
>
>     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;
>
> is "protected type A_Type" a valid completion of "type A_Type is limited
> private;" or is that irrelevant?

Yes, it's a valid completion.

For finalization, it doesn't matter whether a type is private or not.
The finalizations that need to be performed on an object need to be
done regardless of whether the full view of the type is visible at any
particular point.  So if finalization takes place as expected when the
protected type is public, but doesn't take place when it's private,
that's a clear indication that the compiler has blown it.

                                 -- Adam



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Protected object which never get finalized
  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
  1 sibling, 1 reply; 9+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2010-02-15 22:26 UTC (permalink / raw)


Le Mon, 15 Feb 2010 21:23:19 +0100, John B. Matthews  
<nospam@nospam.invalid> a écrit:
> is "protected type A_Type" a valid completion of "type A_Type is limited
> private;" or is that irrelevant?
Nice reaction (you've asked the good question).

The way you've worded this question, I guess you have an RM close to  
hands, so I will give you an answer using formal references :

A short foreword to this reply : the answer to this question is not really  
explicit in the RM, and your question mostly get an implied answer (not  
explicit one), but you'll still understand.

[ARM 7.3(4)] says :
> A private_type_declaration or private_extension_declaration declares
> a partial view of the type; such a declaration is allowed only as a
> declarative_item of the visible part of a package, and it requires a
> completion, which shall be a full_type_declaration that occurs as a
> declarative_item of the private part of the package.
The completion you've talked about ...

Then [ARM 3.2.1(3)] says :
> full_type_declaration ::=
>      type defining_identifier [known_discriminant_part] is  
> type_definition;
>    | task_type_declaration
>    | protected_type_declaration
Protected type declarations allowed as full type declaration, which as the  
latter said, is what (the full type declaration) is expected as the  
completion of a partial view. So, so far, protected types allowed here.

Then after again [ARM 7.3(6/2)] says :
> [...] If the partial view is nonlimited, then the full view shall be
> nonlimited. If a tagged partial view is limited, then the full view
> shall be limited. [...]
Here comes the implicit answer : it only requires that if a if the partial  
view is non-limited, the full-view must not be limited (otherwise, this  
would be contradictory). The partial view here is limited, so the  
completion may be either limited or none limited if the type completion is  
non-tagged, or required to be limited, if the type is tagged.

Protected type are a bit a special things : they are not tagged type which  
you can inherit from, but they can implement interface abstract types. But  
don't bother other it, and look next, you will understand why this seeming  
ambiguity about the tagged/non-tagged status of a protected type, does not  
imply an ambiguous answer here.

Finally [ARM 7.5(3/2,4/2)] says :
> A type is limited if it is a descendant of one of the following:
> * a type with the reserved word limited, synchronized, task, or
>   protected in its definition;
So whatever you consider protected type as being tagged or non-tagged, as  
they are limited by definition, they can be the completion/full-definition  
of a type whose public view is limited.

As you see, the core answer is mainly provided, implicitly (as an implied  
consequence) in [ARM 7.3(6/2)]

> You're welcome; thank you for an interesting example.
You're welcome too

-- 
No-no, this isn't an oops ...or I hope (TM) - Don't blame me... I'm just  
not lucky



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Protected object which never get finalized
  2010-02-15 22:26       ` Hibou57 (Yannick Duchêne)
@ 2010-02-16  0:23         ` John B. Matthews
  0 siblings, 0 replies; 9+ messages in thread
From: John B. Matthews @ 2010-02-16  0:23 UTC (permalink / raw)


In article <op.u76wltl8hgxj9a@garhos>,
 Hibou57 (Yannick Duchêne) <yannick_duchene@yahoo.fr> wrote:

> > is "protected type A_Type" a valid completion of "type A_Type is limited
> > private;" or is that irrelevant?
> Nice reaction (you've asked the good question).
> 
> The way you've worded this question, I guess you have an RM close to  
> hands, so I will give you an answer using formal references :

[ARM 3.2.1(3)]<http://www.adaic.com/standards/05rm/html/RM-3-2-1.html>
[ARM 7.3(4)]<http://www.adaic.com/standards/05rm/html/RM-7-3.html>
[ARM 7.3(6/2)]<http://www.adaic.com/standards/05rm/html/RM-7-3.html>
[ARM 7.5(3/2,4/2)]<http://www.adaic.com/standards/05rm/html/RM-7-5.html>

Thanks, both. I was looking at [ARM 3.11.1] "Completions of 
Declarations" and overlooked [ARM 3.2.1] "Type Declarations".

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-02-16  0:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-13  0:45 Protected object which never get finalized Hibou57 (Yannick Duchêne)
2010-02-14 12:35 ` 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

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