destructors and GC again

Derek Parnell derek at nomail.afraid.org
Mon Oct 16 18:34:15 PDT 2006


On Mon, 16 Oct 2006 20:08:30 -0500, Carlos Santander wrote:

> Derek Parnell escribió:
>> On Mon, 16 Oct 2006 15:34:22 -0500, Carlos Santander wrote:
>> 
>>> There's also the problem with global objects:
>>>
>>> //-------------------------------------------
>>> import std.stdio;
>>>
>>> class A
>>> {
>>>      ~this()
>>>      {
>>>          writefln("~A");
>>>      }
>>> }
>>>
>>> A tmp;
>>>
>>> void main()
>>> {
>>>      tmp=new A;
>>> }
>>> //-------------------------------------------
>>>
>>> Nothing is printed.
>> 
>> Yes, this is my experience too. It also happens with objects stored in AA's
>> I think.  However, either of these fix the issue.
>> 
>>  void main()
>>  {
>>      auto tmp=new A;
>>  }
>> 
>>  . . . .
>> 
>>  void main()
>>  {
>>      auto tmp=new A;
>>      scope(exit) { delete tmp; }
>>  }
>> 
> 
> Of course, but sometimes "tmp" has to be global, so it's not good all the time.

What do you mean? Doesn't "global" mean that the variable needs to be
accessible until main() (or any module dtor for the 'main' module) exits?

Thus this is also okay ...

  static ~this() { delete tmp; }

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
17/10/2006 11:32:08 AM



More information about the Digitalmars-d-learn mailing list