January 31, 2006
----- Original Message ----- 
From: "Jarrett Billingsley" <kb3ctd2@yahoo.com>
Newsgroups: digitalmars.D.learn
Sent: Tuesday, January 31, 2006 12:33 PM
Subject: Re: Releasing resources
> However, in one of my programs, I must delete all instances of a class manually or else their dtors don't get called.. I'll look into why that's happening.

Oh-ho!

class A
{
 this()
 {
  writefln("A ctor");
  as ~= this;
 }

 ~this()
 {
  writefln("A dtor");
 }

 static A[] as;
}

class B
{
 this()
 {
  writefln("B ctor");
 }

 ~this()
 {
  writefln("B dtor");
 }
}

int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpszCmdLine, INT nCmdShow)
{
 A a = new A();
 B b = new B();
 return 0;
}

(using the same WinMain as my last post)

Prints:

A ctor
B ctor
gc_term()
B dtor
gc_term() end

Notice how references to all instances of A are kept in a static array, while references to B are not.  This is what I'm doing in my program (I'm using an AA, though), and it seems to keep the GC from collecting those objects.

Unless I'm committing some stupid mistake, is this a shortcoming of the GC?


January 31, 2006
"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:dro7uu$1ajf$1@digitaldaemon.com...
> Notice how references to all instances of A are kept in a static array, while references to B are not.  This is what I'm doing in my program (I'm using an AA, though), and it seems to keep the GC from collecting those objects.

More testing, and it just seems that any references stored in static arrays are not collected.  I'm not sure, but the docs may hint at this at the bottom, where it talks about the GC not being able to reclaim memory if a root to a large data structure is kept, and then says that "this advice only applies to static references."


January 31, 2006
On Tue, 31 Jan 2006 12:56:41 -0500, Jarrett Billingsley wrote:

> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:dro7uu$1ajf$1@digitaldaemon.com...
>> Notice how references to all instances of A are kept in a static array, while references to B are not.  This is what I'm doing in my program (I'm using an AA, though), and it seems to keep the GC from collecting those objects.
> 
> More testing, and it just seems that any references stored in static arrays are not collected.  I'm not sure, but the docs may hint at this at the bottom, where it talks about the GC not being able to reclaim memory if a root to a large data structure is kept, and then says that "this advice only applies to static references."

Oh dear ... I use that technique in Build. I better manually delete the static array references when it ends I suppose.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
1/02/2006 9:58:40 AM
February 01, 2006
Jarrett Billingsley escribió:
> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:dro7uu$1ajf$1@digitaldaemon.com...
>> Notice how references to all instances of A are kept in a static array, while references to B are not.  This is what I'm doing in my program (I'm using an AA, though), and it seems to keep the GC from collecting those objects.
> 
> More testing, and it just seems that any references stored in static arrays are not collected.  I'm not sure, but the docs may hint at this at the bottom, where it talks about the GC not being able to reclaim memory if a root to a large data structure is kept, and then says that "this advice only applies to static references." 
> 
> 

This also fails:

import std.stdio;

class A
{
    ~this()
    {
        writefln("~A");
    }
}

A tmp;

void main()
{
    tmp=new A;
}

-- 
Carlos Santander Bernal
February 01, 2006
"Carlos Santander" <csantander619@gmail.com> wrote in message news:drov9q$284g$3@digitaldaemon.com...
> This also fails:
>
> import std.stdio;
>
> class A
> {
>     ~this()
>     {
>         writefln("~A");
>     }
> }
>
> A tmp;
>
> void main()
> {
>     tmp=new A;
> }

Then I guess this does conform to the spec, in that static references cannot be collected by the GC.

My question, then, is why can't it?  Or at least, why can't it in gc_term(), when it _knows_ that all objects must be deleted, whether or not their references are in the static data segment?


February 01, 2006
Jarrett Billingsley wrote:
<snip>
> Prints:
> 
> A ctor
> B ctor
> gc_term()
> B dtor
> gc_term() end
> 
> Notice how references to all instances of A are kept in a static array, while references to B are not.  This is what I'm doing in my program (I'm using an AA, though), and it seems to keep the GC from collecting those objects.
> 
> Unless I'm committing some stupid mistake, is this a shortcoming of the GC? 

Looks like a bug to me.  It ought to just look through the heap and destruct everything that's left without caring about references whatsoever.

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:- C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
February 01, 2006
Stewart Gordon wrote:
> Jarrett Billingsley wrote:
> <snip>
>> Prints:
>>
>> A ctor
>> B ctor
>> gc_term()
>> B dtor
>> gc_term() end
>>
>> Notice how references to all instances of A are kept in a static array, while references to B are not.  This is what I'm doing in my program (I'm using an AA, though), and it seems to keep the GC from collecting those objects.
>>
>> Unless I'm committing some stupid mistake, is this a shortcoming of the GC? 
> 
> Looks like a bug to me.  It ought to just look through the heap and destruct everything that's left without caring about references whatsoever.

It should probably look through the stack as well.  The difference in this case being that should merely call all dtors it can find--freeing memory shouldn't matter since the application is shutting down anyway.


Sean
1 2
Next ›   Last »