www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - GC.collect bug ?

reply "Temtaime" <temtaime gmail.com> writes:
Hello for all !

I need to call all objects destructors in one place.
It's guaranted, that there is no objects instances.

I tried use GC.collect but it's produces strange results.

import std.stdio;
import core.memory;

class A {
	~this() { writeln(`dtor`); };
};

void main() {
	auto a = new A;
	a = null;

	GC.collect();
	writeln(`after dtor`);
}

Output is:
after dtor
dtor

Why?

Regards.
Sep 13 2013
next sibling parent reply "evilrat" <evilrat666 gmail.com> writes:
On Friday, 13 September 2013 at 15:24:17 UTC, Temtaime wrote:
 Hello for all !

 I need to call all objects destructors in one place.
 It's guaranted, that there is no objects instances.

 I tried use GC.collect but it's produces strange results.

 import std.stdio;
 import core.memory;

 class A {
 	~this() { writeln(`dtor`); };
 };

 void main() {
 	auto a = new A;
 	a = null;

 	GC.collect();
 	writeln(`after dtor`);
 }

 Output is:
 after dtor
 dtor

 Why?

 Regards.
this is probably because nothing collected until reference in main function goes from scope. last time i tried GC.collect it wasn't do immediate GC run, AFAIK you can't force it to do so in predictable manner.
Sep 13 2013
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
Another use case of delete, which is unfortunately deprecated. ;)
----
import std.stdio;
import core.memory;

class A {
	~this() { writeln(`dtor`); };
};

void main() {
	auto a = new A;
	delete a;
	writeln(`after dtor`);
}
----

Ooutput:
dtor
after dtor
Sep 13 2013
parent reply "sclytrack" <sclytrack idiot.com> writes:
On Friday, 13 September 2013 at 16:43:01 UTC, Namespace wrote:
 Another use case of delete, which is unfortunately deprecated. 
 ;)
 ----
 import std.stdio;
 import core.memory;

 class A {
 	~this() { writeln(`dtor`); };
 };

 void main() {
 	auto a = new A;
 	delete a;
 	writeln(`after dtor`);
 }
 ----

 Ooutput:
 dtor
 after dtor
//http://dlang.org/memory.html import std.c.stdlib; import core.exception; import core.memory : GC; import std.stdio; class TObject { new(size_t sz) { void* p; p = std.c.stdlib.malloc(sz); if (!p) throw new OutOfMemoryError(); GC.addRange(p, sz); writeln("Memory Allocation", sz); return p; } delete(void* p) { if (p) { GC.removeRange(p); std.c.stdlib.free(p); writeln("Memory Removal"); } } } class TComponent: TObject { int number; } int main() { TComponent obj = new TComponent(); delete obj; writeln("testing the code"); return 0; } //What is the replacement for delete?
Sep 14 2013
parent "Namespace" <rswhite4 googlemail.com> writes:
On Saturday, 14 September 2013 at 17:22:51 UTC, sclytrack wrote:
 On Friday, 13 September 2013 at 16:43:01 UTC, Namespace wrote:
 Another use case of delete, which is unfortunately deprecated. 
 ;)
 ----
 import std.stdio;
 import core.memory;

 class A {
 	~this() { writeln(`dtor`); };
 };

 void main() {
 	auto a = new A;
 	delete a;
 	writeln(`after dtor`);
 }
 ----

 Ooutput:
 dtor
 after dtor
//http://dlang.org/memory.html import std.c.stdlib; import core.exception; import core.memory : GC; import std.stdio; class TObject { new(size_t sz) { void* p; p = std.c.stdlib.malloc(sz); if (!p) throw new OutOfMemoryError(); GC.addRange(p, sz); writeln("Memory Allocation", sz); return p; } delete(void* p) { if (p) { GC.removeRange(p); std.c.stdlib.free(p); writeln("Memory Removal"); } } } class TComponent: TObject { int number; } int main() { TComponent obj = new TComponent(); delete obj; writeln("testing the code"); return 0; } //What is the replacement for delete?
AFAIK Andrei would like to see if D get rid of new and delete overloading. Especially since delete is deprecated. The replacement is 'destroy'. It also call the DTor but it does not free the memory. As soon as delete is fully removed we have to trust the broken GC.
Sep 14 2013
prev sibling parent reply "Temtaime" <temtaime gmail.com> writes:
I cannot use the delete/destroy. I want to call dtor at all 
unreferenced objects.
Manual from Dlang size says that GC.collect triggers a full 
collection. But it doesn't.
Sep 17 2013
next sibling parent Sean Kelly <sean invisibleduck.org> writes:
On Sep 17, 2013, at 4:14 AM, Temtaime <temtaime gmail.com> wrote:

 I cannot use the delete/destroy. I want to call dtor at all =
unreferenced objects.
 Manual from Dlang size says that GC.collect triggers a full =
collection. But it doesn't. It does. But the collector isn't guaranteed to collect everything that = is no longer referenced during a given collection cycle. It's possible = a register still holds the reference to that object. Try doing a bit = more stuff before collecting and see if that changes behavior. Or = allocate a second dummy object.=
Sep 17 2013
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Tuesday, 17 September 2013 at 11:14:10 UTC, Temtaime wrote:
 I cannot use the delete/destroy. I want to call dtor at all 
 unreferenced objects.
 Manual from Dlang size says that GC.collect triggers a full 
 collection. But it doesn't.
It is not possible by design as GC is not deterministic. Destructors are not guaranteed to be ever run at all. You need to change your program architecture if you rely on it right now.
Sep 18 2013
prev sibling next sibling parent "thedeemon" <dlang thedeemon.com> writes:
On Friday, 13 September 2013 at 15:24:17 UTC, Temtaime wrote:
 	auto a = new A;
 	a = null;
 	GC.collect();
I suspect the compiler sees that "a" isn't used anywhere down the function, so it optimizes away "a = null", hence it's not null when GC happens. Better look at generated assembly to be sure.
Sep 15 2013
prev sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Friday, 13 September 2013 at 15:24:17 UTC, Temtaime wrote:
 Hello for all !

 I need to call all objects destructors in one place.
 It's guaranted, that there is no objects instances.

 I tried use GC.collect but it's produces strange results.

 import std.stdio;
 import core.memory;

 class A {
 	~this() { writeln(`dtor`); };
 };

 void main() {
 	auto a = new A;
 	a = null;

 	GC.collect();
 	writeln(`after dtor`);
 }

 Output is:
 after dtor
 dtor

 Why?

 Regards.
For me it prints dtor - after dtor in case of pure compilation. In general it depends on compiler switches and druntime.
Sep 15 2013