comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Finalization of static package variables
Date: 9 May 2007 16:27:08 -0700
Date: 2007-05-09T16:27:08-07:00	[thread overview]
Message-ID: <1178753228.648765.89390@e65g2000hsc.googlegroups.com> (raw)
In-Reply-To: <464244d8$1@news.upm.es>

On May 9, 3:00 pm, Manuel Collado <m.coll...@lml.ls.fi.upm.es> wrote:
> Adam Beneschan escribió:

> Well, JEWL uses tasks internally. So, who knows...

> > No, I believe that all objects, including static package variables
> > *and* including allocated objects that have not yet been deallocated,
> > are supposed to be finalized when the environment task completes.
> > 10.2(25), 7.6.1(10).
>
> The fact is that there is a significant difference in behaviour between
> variables in the main procedure and static package variables. Example:
>
> 1. The following code always terminates smoothly.
>
> --- main1.adb ---------------------------------------
> with Jewl.Simple_Windows; use Jewl.Simple_Windows;
>
> procedure Main1 is
>     My_Frame: Frame_Type := Frame (200, 100, "Frame 1", 'Q');
>     Ok: Button_Type := Button (My_Frame, (50, 20), 80, 25, "OK", 'Q');
>     C: Character;
> begin
>     C := Next_Command;        -- just wait for any 'frame' event
> end Main1;
> ----------------------------------------------------
>
> 2. The following alternate code may not terminate (remains as a zombie
> process). Requires explicit termination of the Frame, either by the [X]
> at the top-rigth corner or by 'Close(My_Frame)'.
>
> --- main2.adb ---------------------------------------
> procedure Main2 is
> begin
>     Do_Frame; -- just wait for any GUI event
> end Main2;
>
> --- pkg_frame.ads ---------------------------------------
> package Pkg_Frame is
>     procedure Do_Frame;
> end Pkg_Frame;
>
> --- main1.adb ---------------------------------------
> with Jewl.Simple_Windows; use Jewl.Simple_Windows;
>
> package body Pkg_Frame is
>     My_Frame: Frame_Type := Frame (200, 100, "Frame 2", 'Q');
>     Ok: Button_Type := Button (My_Frame, (50, 20), 80, 25, "OK", 'Q');
>     C: Character;
>
>     procedure Do_Frame is
>     begin
>        C := Next_Command; -- just wait for any GUI event
>        -- POSSIBLE IMPROPER TERMINATION --
>        -- may be fixed by enabling the following statement
>        -- Close (My_Frame);
>     end Do_Frame;
>
> end Pkg_Frame;
> ----------------------------------------------------
>
> Could the My_Frame declaration scope interfere with possible
> unterminated tasks inside JEWL?

Maybe.  I don't know anything about JEWL, so I don't know what the
declaration of "Frame_Type" is or what the "Frame" function does.  But
if the Frame function uses an allocator to create an access-to-task,
and if Frame_Type is a controlled type (or has controlled components)
whose Finalize routine does something that terminates that task, then
I can see how you would have a problem.  According to 9.3, if you (or
some routine in JEWL) have started a task with an allocator, then that
task depends on the master that elaborates the access-to-task *type*
(and any enclosing master).  Assuming the access type is declared at
the library level somewhere inside JEWL, then the only master it
depends on is the environment task.

When you declare My_Frame inside your main procedure, then as soon as
the procedure completes, My_Frame is finalized.  I'm assuming that its
Finalize routine (or the Finalize routine of a component) has code
that causes any task created by My_Frame's initialization to
terminate.  So this will cause things to terminate cleanly.

When you declare My_Frame at the library level, you've got a Catch-22
problem.  10.2(25) says, "When the environment task completes..., it
waits for the termination of all such tasks, and then finalizes any
remaining objects of the partition".  Again, assuming My_Frame's
finalize would terminate the task, the task termination would need to
be performed by the object finalization; but 10.2(25) says that task
termination must come *first*, before object finalization.  Yikes!

So it does look like you need to finalize My_Frame explicitly
(probably by calling Close).  It seems like there's a flaw somewhere.
Possibly, JEWL needs a "shutdown" routine to terminate any tasks that
are still running.  Offhand, though, I don't see any good way to
rearrange your program to make sure library-level objects are
finalized *before* the tasks are terminated, except by making sure all
your Frame_Type objects and such are declared inside some procedure,
which probably means making a lot of your packages subunits of some
big procedure, which is probably obnoxious.  I don't think Ada has a
way to tell the environment task that some objects, or objects of some
specified types, should be finalized *before* waiting for task
termination.  It would be helpful here, but there may not be a good
way to add a language feature to facilitate this.

                                         -- Adam





  reply	other threads:[~2007-05-09 23:27 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-08 21:55 Finalization of static package variables Manuel Collado
2007-05-09  8:13 ` Alex R. Mosteo
2007-05-09  8:20 ` Stephen Leake
2007-05-09 15:15   ` Adam Beneschan
2007-05-09 16:01     ` Markus E Leypold
2007-05-09 22:00     ` Manuel Collado
2007-05-09 23:27       ` Adam Beneschan [this message]
2007-05-10  4:52         ` Jeffrey R. Carter
2007-05-16  0:41           ` Randy Brukardt
2007-05-10  8:16         ` Alex R. Mosteo
2007-05-16  0:36         ` Randy Brukardt
2007-05-16 14:06           ` Adam Beneschan
2007-05-16 16:47             ` Jeffrey R. Carter
2007-05-16 19:10             ` Randy Brukardt
2007-05-22  4:41               ` christoph.grein
2007-05-22  6:25                 ` tmoran
2007-05-22 22:19                   ` Randy Brukardt
2007-05-10 10:57       ` Stephen Leake
2007-05-09 22:19   ` Manuel Collado
2007-05-10  5:38     ` AW: " Grein, Christoph (Fa. ESG)
2007-05-10 10:55     ` Stephen Leake
2007-05-11 18:12       ` Manuel Collado
2007-05-11 18:26         ` Robert A Duff
2007-05-12 19:17           ` Finalization of static package variables - summary Manuel Collado
2007-05-16  0:49             ` 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