Jump to page: 1 2 3
Thread overview
Finalize GC memory
Jun 15, 2013
Namespace
Jun 16, 2013
Namespace
Jun 16, 2013
Ali Çehreli
Jun 16, 2013
Namespace
Release GC memory
Jun 16, 2013
Namespace
Jun 16, 2013
Jonathan M Davis
Jun 16, 2013
Namespace
Jun 16, 2013
Namespace
Jun 17, 2013
Namespace
Jun 17, 2013
Jonathan M Davis
Jun 18, 2013
Namespace
Jun 18, 2013
Namespace
Jul 04, 2013
Namespace
Jun 17, 2013
Namespace
Jun 18, 2013
Namespace
Jun 18, 2013
bearophile
Jun 18, 2013
Namespace
Jun 18, 2013
Namespace
June 15, 2013
With the knowledge that 'delete' will be deprecated soon:
Is there then still a way to finalize GC memory? I know it's unsafe etc. but is there a way? I know of 'destroy' but it does not finalize anything and AFAIK it's not safe and has also side effects. And it does not call the DTor if any struct is allocated with 'new'.
I tried GC.free, GC.collect, destroy and a few other things, but except of 'delete' nothing finalize the memory.
Thoughts?
June 16, 2013
I assume the silence means no?
June 16, 2013
On 06/15/2013 02:22 PM, Namespace wrote:

> With the knowledge that 'delete' will be deprecated soon:
> Is there then still a way to finalize GC memory? I know it's unsafe etc.
> but is there a way? I know of 'destroy' but it does not finalize
> anything and AFAIK it's not safe and has also side effects.

Being a C++ programmer, I am new to the term "finalize". If it means "call the destructor", then destroy() is it.

> And it does
> not call the DTor if any struct is allocated with 'new'.

It does:

import std.stdio;

struct S
{
    ~this()
    {
        writeln("destructor called");
    }
}

void main()
{
    auto s = new S();
    destroy(*s);        // NOTE the '*' operator!
}

> I tried GC.free, GC.collect, destroy and a few other things, but except
> of 'delete' nothing finalize the memory.
> Thoughts?

Ali

June 16, 2013
> Being a C++ programmer, I am new to the term "finalize". If it means "call the destructor", then destroy() is it.
Finalize -> free the memory immediately as 'delete' it does currently.

> > And it does
> > not call the DTor if any struct is allocated with 'new'.
>
> It does:
>
> import std.stdio;
>
> struct S
> {
>     ~this()
>     {
>         writeln("destructor called");
>     }
> }
>
> void main()
> {
>     auto s = new S();
>     destroy(*s);        // NOTE the '*' operator!
> }

Good to know!

June 16, 2013
I have adjusted the thread title.
June 16, 2013
On Sunday, June 16, 2013 12:24:40 Ali Çehreli wrote:
> On 06/15/2013 02:22 PM, Namespace wrote:
>  > With the knowledge that 'delete' will be deprecated soon:
>  > Is there then still a way to finalize GC memory? I know it's unsafe etc.
>  > but is there a way? I know of 'destroy' but it does not finalize
>  > anything and AFAIK it's not safe and has also side effects.
> 
> Being a C++ programmer, I am new to the term "finalize". If it means "call the destructor", then destroy() is it.

http://en.wikipedia.org/wiki/Finalizer

It's basically just means calling the finalizer, and D conflates finalizers and destructors, so the destructor of any object on the GC heap is that object's finalizer. Normally, it's called when the object is collected, but it can get a bit messy depending on how the GC works. It's essentially a non-determinstic destructor though.

- Jonathan M Davis
June 16, 2013
> http://en.wikipedia.org/wiki/Finalizer
>
> It's basically just means calling the finalizer, and D conflates finalizers and
> destructors, so the destructor of any object on the GC heap is that object's
> finalizer. Normally, it's called when the object is collected, but it can get a
> bit messy depending on how the GC works. It's essentially a non-determinstic
> destructor though.
>
> - Jonathan M Davis

It was my fault. What I meant was to release GC memory immediately.
June 16, 2013
It seems that does what I want. The result is the same as with the current 'delete' implementation.

----
void Delete(T)(ref T var) {
	static if (is(T == struct) && is(typeof(var.__dtor)))
		var.__dtor();
		
	static if (is(T : U[], U))
		core.memory.GC.free(var.ptr);
	else {
		static if (is(T : U*, U))
			core.memory.GC.free(var);
		else
			core.memory.GC.free(&var);
	}
}
----

But if I call 'destroy' before I call GC.free, it does not work correct.
June 17, 2013
On Sunday, 16 June 2013 at 21:37:16 UTC, Namespace wrote:
> It seems that does what I want. The result is the same as with the current 'delete' implementation.
>
> ----
> void Delete(T)(ref T var) {
> 	static if (is(T == struct) && is(typeof(var.__dtor)))
> 		var.__dtor();
> 		
> 	static if (is(T : U[], U))
> 		core.memory.GC.free(var.ptr);
> 	else {
> 		static if (is(T : U*, U))
> 			core.memory.GC.free(var);
> 		else
> 			core.memory.GC.free(&var);
> 	}
> }
> ----
>
> But if I call 'destroy' before I call GC.free, it does not work correct.

I forget 'var = null;'.

But:
Any thoughts on this?
Will it work as I want?
In my few tests it seems to work like delete, but I would be interested in the meaning of some experts.
June 17, 2013
On Monday, June 17, 2013 18:46:29 Namespace wrote:
> On Sunday, 16 June 2013 at 21:37:16 UTC, Namespace wrote:
> > It seems that does what I want. The result is the same as with the current 'delete' implementation.
> > 
> > ----
> > void Delete(T)(ref T var) {
> > 
> > static if (is(T == struct) && is(typeof(var.__dtor)))
> > 
> > var.__dtor();
> > 
> > static if (is(T : U[], U))
> > 
> > core.memory.GC.free(var.ptr);
> > 
> > else {
> > 
> > static if (is(T : U*, U))
> > 
> > core.memory.GC.free(var);
> > 
> > else
> > 
> > core.memory.GC.free(&var);
> > 
> > }
> > 
> > }
> > ----
> > 
> > But if I call 'destroy' before I call GC.free, it does not work correct.
> 
> I forget 'var = null;'.
> 
> But:
> Any thoughts on this?
> Will it work as I want?
> In my few tests it seems to work like delete, but I would be
> interested in the meaning of some experts.

I'd be worried about whether it actually called the destructors of the members of the struct. IIRC, you actually have to play around with TypeInfo in order to be able to correctly manually destroy a struct rather than simply calling its __dtor method, but it's not something that I ever do, so I don't remember the details. Regardless, if destroy doesn't actually destroy a struct correctly, then that's a bug and should be reported.

- Jonathan M Davis
« First   ‹ Prev
1 2 3