Re: GC.collect bug ?

2013-09-18 Thread Dicebot

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.


Re: GC.collect bug ?

2013-09-17 Thread Temtaime
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.


Re: GC.collect bug ?

2013-09-17 Thread Sean Kelly
On Sep 17, 2013, at 4:14 AM, Temtaime temta...@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.

Re: GC.collect bug ?

2013-09-15 Thread thedeemon

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.


Re: GC.collect bug ?

2013-09-15 Thread Maxim Fomin

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.


Re: GC.collect bug ?

2013-09-14 Thread sclytrack

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?


Re: GC.collect bug ?

2013-09-14 Thread Namespace

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.


GC.collect bug ?

2013-09-13 Thread Temtaime

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.


Re: GC.collect bug ?

2013-09-13 Thread Namespace

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


Re: GC.collect bug ?

2013-09-13 Thread evilrat

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.