comp.lang.ada
 help / color / mirror / Atom feed
* Class with task destructor
@ 2011-11-23  1:50 Rego, P.
  2011-11-23  2:44 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 50+ messages in thread
From: Rego, P. @ 2011-11-23  1:50 UTC (permalink / raw)


So now I have a class with a task inside and a Constructor, and I want to implement a destructor.

--test_pkg.ads
with Unchecked_Deallocation;
package Test_Pkg is

   type Test_Class is tagged;
   type Test_Class_Ptr is access all Test_Class;
   
   function Construct return Test_Class_Ptr;
   procedure Destruct (T : access Test_Class);
 
   task type Primary_Task (This : not null access Test_Class) is 
      entry Start;
      entry Pause;
      entry Release;
      entry Finish;
   end Primary_Task;
   
   type Test_Class is tagged limited
      record
         Some_Info : Integer;
         Primary   : Primary_Task (Test_Class'Access);
      end record;
   
   procedure Free is new Unchecked_Deallocation (Object => Test_Class,
                                                 Name   => Test_Class_Ptr);
end Test_Pkg;

--test_pkg_main.adb
with Test_Pkg; use Test_Pkg;

procedure Test_Pkg_Main is
   Obj_Ptr : Test_Class_Ptr;
begin
   Obj_Ptr := Construct;
   delay 5.0;
   Obj_Ptr.Destruct;

end Test_Pkg_Main;

--test_pkg.adb
with Text_IO; use Text_IO;
package body Test_Pkg_Rev661 is
   task body Primary_Task is
   begin
      Outer_Loop : loop
         select
            accept Start;
            Main_Loop : loop
               select
                  accept Pause;
                  select
                     accept Release;
                  or
                     accept Finish;
                     exit Outer_Loop;
                  or
                     terminate;
                  end select;
               or
                  accept Finish;
                  exit Outer_Loop;
               else
                  Put ("^"&Integer'Image (This.Some_Info));
                  delay 0.5;
               end select;
            end loop Main_Loop;
         or
            terminate;
         end select;
      end loop Outer_Loop;
   end Primary_Task;

   function Construct return Test_Class_Ptr is
      T_Ptr : constant Test_Class_Ptr := new Test_Class;
   begin
      T_Ptr.Some_Info := 1;
      T_Ptr.Primary.Start;
      return T_Ptr;
   end Construct;

   procedure Destruct (T : access Test_Class) is
   ...
   end Destruct;
end Test_Pkg_Rev661;

--
(1) So, the first implementation I tried was
   procedure Destruct (T : access Test_Class) is
      T_Ptr : Test_Class_Ptr := Test_Class_Ptr (T);
   begin
      Free (T_Ptr);
   end Destruct;

So I thought that if I unallocate the object, the task inside would be dead too, because the object would be its master. It looks that I'm wrong, because the execution led me to

^ 1^ 1^ 1^ 1^ 1^ 1^ 1^ 1^ 1^ 1^ 6295912^ 6295912^ (...)
then the task is not dead after the object dies. 
So why is this implementation wrong?

(2) The second one implementation I tried, which looks me that led me to a (happy) program termination was 
   procedure Destruct (T : access Test_Class) is
      T_Ptr : Test_Class_Ptr := Test_Class_Ptr (T);
   begin
      T.Primary.Finish;
      Free (T_Ptr);
   end Destruct;

and the execution leds me to
^ 1^ 1^ 1^ 1^ 1^ 1^ 1^ 1^ 1^ 1 (and no more)

I agree that in this case the task is dead, because of the forced termination. And so on the object itself. But is this implementation preferable over the first one?

Thank you again.



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

end of thread, other threads:[~2011-12-02 20:01 UTC | newest]

Thread overview: 50+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-23  1:50 Class with task destructor Rego, P.
2011-11-23  2:44 ` Adam Beneschan
2011-11-23  5:04   ` Yannick Duchêne (Hibou57)
2011-11-23  6:14     ` Adam Beneschan
2011-11-24  0:15       ` Randy Brukardt
2011-11-24  2:48         ` Adam Beneschan
2011-11-29  3:36           ` Randy Brukardt
2011-11-29  9:31             ` Simon Wright
2011-11-29 15:37             ` Adam Beneschan
2011-11-23  8:35 ` Dmitry A. Kazakov
2011-11-23  9:05   ` Simon Wright
2011-11-23 10:41     ` Dmitry A. Kazakov
2011-11-30  1:11     ` Rego, P.
2011-11-30  2:21       ` Adam Beneschan
2011-11-30  8:41         ` Dmitry A. Kazakov
2011-12-01  0:35           ` Randy Brukardt
2011-12-01  6:28             ` J-P. Rosen
2011-12-01 10:55               ` Simon Wright
2011-12-01 21:48               ` Robert A Duff
2011-12-01 22:44                 ` Adam Beneschan
2011-12-02  0:57                 ` Randy Brukardt
2011-12-02  5:57                 ` J-P. Rosen
2011-12-02 15:07                   ` Robert A Duff
2011-12-02 18:41                   ` Jeffrey Carter
2011-12-01  9:25             ` Dmitry A. Kazakov
2011-12-01  1:58         ` Rego, P.
2011-11-30  8:35       ` Simon Wright
2011-11-30 15:36         ` Adam Beneschan
2011-11-30 16:32           ` Robert A Duff
2011-12-01  0:40             ` Randy Brukardt
2011-12-01  8:50               ` Yannick Duchêne (Hibou57)
2011-12-02  0:50                 ` Randy Brukardt
2011-12-02  5:30                   ` Jeffrey Carter
2011-12-02 16:20                     ` Adam Beneschan
2011-12-02 18:01                       ` Dmitry A. Kazakov
2011-12-02 18:50                       ` Jeffrey Carter
2011-12-02 19:03                         ` Adam Beneschan
2011-12-01 10:51           ` Simon Wright
2011-12-01 22:59             ` Simon Wright
2011-12-01  1:59         ` Rego, P.
2011-11-30  1:47     ` Rego, P.
     [not found]     ` <15090042.1880.1322617401962.JavaMail.geo-discussion-forums@yqkn8>
2011-11-30  8:43       ` Dmitry A. Kazakov
2011-12-01  1:53         ` Rego, P.
2011-12-01  9:28           ` Dmitry A. Kazakov
2011-11-25  2:44   ` Rego, P.
     [not found]   ` <28489797.1088.1322188495508.JavaMail.geo-discussion-forums@yqf20>
2011-11-25  9:19     ` Dmitry A. Kazakov
2011-11-29  3:40       ` Randy Brukardt
2011-11-23 10:26 ` Brian Drummond
2011-11-25  1:37   ` Rego, P.
2011-11-25 13:40     ` Brian Drummond

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