From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,4bd6ca8f7a1eb225,start X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.35.131 with SMTP id h3mr2132674pbj.1.1322013045573; Tue, 22 Nov 2011 17:50:45 -0800 (PST) Path: lh20ni7337pbb.0!nntp.google.com!news2.google.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: "Rego, P." Newsgroups: comp.lang.ada Subject: Class with task destructor Date: Tue, 22 Nov 2011 17:50:45 -0800 (PST) Organization: http://groups.google.com Message-ID: <30604696.94.1322013045135.JavaMail.geo-discussion-forums@yqzz20> Reply-To: comp.lang.ada@googlegroups.com NNTP-Posting-Host: 189.110.14.179 Mime-Version: 1.0 X-Trace: posting.google.com 1322013045 23866 127.0.0.1 (23 Nov 2011 01:50:45 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 23 Nov 2011 01:50:45 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=189.110.14.179; posting-account=TRgI1QoAAABSsYi-ox3Pi6N-JEKKU0cu User-Agent: G2/1.0 X-Google-Web-Client: true Xref: news2.google.com comp.lang.ada:14533 Content-Type: text/plain; charset=ISO-8859-1 Date: 2011-11-22T17:50:45-08:00 List-Id: 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.