destructors and GC again
Derek Parnell
derek at psyc.ward
Mon Oct 16 14:13:30 PDT 2006
On Mon, 16 Oct 2006 15:34:22 -0500, Carlos Santander wrote:
> Sean Kelly escribió:
>> Lutger wrote:
>>> Sorry for this topic again, I'm still not exactly sure: when an
>>> unreferenced object is collected during the lifetime of your program
>>> (before main exits), is the destructor of this object guaranteed to be
>>> called? I expect it to be so, as otherwise destructors seem pretty
>>> useless (and dangerous), but cannot infer this from the spec.
>>
>> When an object is collected by the GC, it's dtor will be called.
>> However, the GC may not detect an orphaned object as collectable if
>> there is a value in memory somewhere that "looks" like a reference to
>> that object.
>>
>> Sean
>
> 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; }
}
--
Derek Parnell
Melbourne, Australia
"Down with mediocrity!"
More information about the Digitalmars-d-learn
mailing list