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.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7833e44c4d0da1bf X-Google-Attributes: gid103376,public From: Mats Weber Subject: Re: Task deallocation Date: 1997/02/28 Message-ID: <3316EB5F.54B7@elca-matrix.ch>#1/1 X-Deja-AN: 222123232 References: <01bc2193$50d867c0$829d6482@joy.ericsson.se> Organization: ELCA Matrix SA Reply-To: Mats.Weber@elca-matrix.ch Newsgroups: comp.lang.ada Date: 1997-02-28T00:00:00+00:00 List-Id: Jonas Nygren wrote: > > I sent out a question about how to deallocate dynamically allocated tasks. > There were some answers from which I learnt that deallocating new'd tasks > was perhaps not so straightforward as I believed. > > Apparently most compilers represents a task by a pointer to some internal > structure. If this internal storage is not reclaimed when a task is > deallocated one will have a storage leak. > > So my question is how to deallocate a dynamically allocated task without > creating a memory leak. > > If I call Free for a dynamically allocated task will this ensure that the > internal storage structures for tasks are reclaimed? E.g.: > > type My_Task_Ref is access My_Task; > procedure Free is new Unchecked_Deallocation(My_Task, My_Task_Ref); > MTR : My_Task_Ref := new My_Task; > ... > Free(MTR); -- assume MTR'Terminated is true > -- will the internal task structures have been reclaimed?? On a reasonable implementation, all memory will be reclaimed when the task terminates. Ada 95 (in 13.11.2) says it is an error to deallocate an unterminated discriminated task object, but doesn't say anything on deallocating a non-discriminated task object. Could anyone clarify this ? But note that it is not so easy to make sure that the task is terminated when you call Free, for instance: task type my_task is entry E; entry Done; end; task body my_task is begin loop accept E; exit when ...; end loop; accept Done; end; ... MTR.Done; Free(MTR); It is not certain that MTR.all'Terminted is true when Free is called (the task can still be between "accept Done" and "end"). To be absolutely sure, you must write MTR.Done; begin MTR.Done; exception when Tasking_Error => Free(MTR); end; which is quite ugly. In Ada 83, freeing a non-terminted task object did not affect the task itself and I have never had a problem doing so in programs (particularly in the above situation). Has this been changed in Ada 95 ? > Is there really no portable way to deallocate dynamically allocated tasks > without storage leaks? Strictly speaking, there is no portable way of deallocating _any_ memory in Ada when you use standard storage pools. The fact that Unchecked_Deallocation effectively deallocates is just an implementation advice. But all reasonable implementations will effectively deallocate, and this should also hold for task memory. Making memory management non-mandatory is quite reasonable if you consider that Ada should also be able to run on embedded CPUs with very little memory where dynamic allocation would not be used and would only increase the size of the run time system. On the other hand, a compiler for UNIX will be very hard to sell if Unchecked_Deallocation does not work (except if it has a garbage collector).