Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 13, 2013 GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
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. |
September 13, 2013 Re: GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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.
|
September 13, 2013 Re: GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to evilrat | 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 |
September 14, 2013 Re: GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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? |
September 14, 2013 Re: GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to sclytrack | 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.
|
September 15, 2013 Re: GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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.
|
September 15, 2013 Re: GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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.
|
September 17, 2013 Re: GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to evilrat | 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. |
September 17, 2013 Re: GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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.
|
September 18, 2013 Re: GC.collect bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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.
|
Copyright © 1999-2021 by the D Language Foundation