| Thread overview | |||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 17, 2007 GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Hi,
I just made some small examples for myself to understand the GC. Here i have a testcase, that has an unexpected behavior for me:
import std.stdio;
static int number = 0;
static int number2 = 0;
class MyClass {
public:
this() { writefln("Constructor %d", number);
myNumber = number; number++;
member = new My2Class(); };
~this() { writefln("Destructor %d", myNumber); };
My2Class member;
int myNumber;
};
class My2Class {
public:
this() { writefln("Sub_Constructor2 %d", number2);
myNumber2 = number2; number2++; };
~this() { writefln("Sub_Destructor2 %d", myNumber2); };
int myNumber2;
};
int main(char[][] args) {
{
scope MyClass A = new MyClass();
}
std.gc.fullCollect();
writefln("The Sub Class from 0 does not get deleted yet!");
MyClass C = new MyClass();
return 0;
};
The Output ist:
Constructor 0
Sub_Constructor2 0
Destructor 0
The Sub Class from 0 does not get deleted yet!
Constructor 1
Sub_Constructor2 1
Sub_Destructor2 1
Destructor 1
Sub_Destructor2 0
| ||||
December 17, 2007 Re: GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Matthias Thurau | BTW:
This is working correctly:
void func() {
MyClass A = new MyClass();
};
int main(char[][] args) {
func();
std.gc.fullCollect();
writefln("The Sub Class from 0 does not get deleted yet!");
MyClass C = new MyClass();
return 0;
};
When i put the "scope" into the func(), then i get the output from above again. The right ouput was in my opinion:
Constructor 0
Sub_Constructor2 0
Sub_Destructor2 0
Destructor 0
The Sub Class from 0 does not get deleted yet!
Constructor 1
Sub_Constructor2 1
Sub_Destructor2 1
Destructor 1
| |||
December 17, 2007 Re: GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Matthias Thurau | Matthias Thurau Wrote:
> When i put the "scope" into the func(), then i get the output from above again.
It looks like a bug to me. Does this happen with Phobos or Tango? (or both?) I know they use different gc's.
| |||
December 17, 2007 Re: GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jason House | Jason House wrote:
> Matthias Thurau Wrote:
>
>> When i put the "scope" into the func(), then i get the output from above again.
>
>
> It looks like a bug to me. Does this happen with Phobos or Tango? (or both?) I know they use different gc's.
I've noticed that the most recent object to be constructed is often not deleted in simple test cases. My guess is that a reference to this address is probably still lingering in a register somewhere, so the GC thinks it's still alive. This happens in Phobos and Tango.
Sean
| |||
December 18, 2007 Re: GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | thanks for replys... So how do i handle this? Should i ignore that or am i doing something wrong? (Ist it a bug or not? :) bye | |||
December 18, 2007 Re: GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Matthias Thurau | Matthias Thurau wrote: > thanks for replys... > > So how do i handle this? Should i ignore that or am i doing something > wrong? (Ist it a bug or not? :) > > bye It is hardly a bug. You should in any case not depend on an object being collected. Garbage collection is not deterministic. -- Lars Ivar Igesund blog at http://larsivi.net DSource, #d.tango & #D: larsivi Dancing the Tango | |||
December 18, 2007 Re: GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Matthias Thurau | Matthias Thurau wrote:
> thanks for replys...
>
> So how do i handle this? Should i ignore that or am i doing something wrong? (Ist it a bug or not? :)
Ignore it. Most probably no bug!
Who has ever claimed that the GC collects objects in any specific order?
regards, frank
| |||
December 18, 2007 Re: GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Posted in reply to 0ffh | 0ffh Wrote:
> Matthias Thurau wrote:
> > thanks for replys...
> >
> > So how do i handle this? Should i ignore that or am i doing something wrong? (Ist it a bug or not? :)
>
> Ignore it. Most probably no bug!
> Who has ever claimed that the GC collects objects in any specific order?
I don't think it was specifically the order but rather full collection not fully collecting.
I haven't tried this, but is it possible to declare class member variables as scope so that they will get deleted when the rest of the object does? This could save coding of custom destructors in some cases.
| |||
December 18, 2007 Re: GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jason House | "Jason House" wrote
> 0ffh Wrote:
>
>> Matthias Thurau wrote:
>> > thanks for replys...
>> >
>> > So how do i handle this? Should i ignore that or am i doing something wrong? (Ist it a bug or not? :)
>>
>> Ignore it. Most probably no bug!
>> Who has ever claimed that the GC collects objects in any specific order?
>
> I don't think it was specifically the order but rather full collection not fully collecting.
>
> I haven't tried this, but is it possible to declare class member variables as scope so that they will get deleted when the rest of the object does? This could save coding of custom destructors in some cases.
Custom destructors should NEVER destroy other GC allocated objects. the GC cannot guarantee the order of collection, meaning if you have a pointer to another GC allocated object, that object may have already been collected.
There is no real reason to delete other objects in a destructor. If the objects are no longer referenced, the GC will destroy those also. The only things you should destroy in a destructor are non-GC allocated resources, such as file descriptors, or memory allocated with C's malloc.
So to answer your question: scope is not necessary as a modifier for a member variable.
-Steve
| |||
December 18, 2007 Re: GC does not delete subclass | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | "If the objects are no longer referenced, the GC will destroy those also." Thats exact the case: In my example the GC isn t destroying that unreferenced member. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply