June 17, 2013
On Sun, 16 Jun 2013 17:37:15 -0400, Namespace <rswhite4@googlemail.com> wrote:

> But if I call 'destroy' before I call GC.free, it does not work correct.

Destroy puts anything back into it's initial state.  So for anything that is a pointer, destroy will set it to null!  Then calling GC free on null is no good.

What you have to do is save a copy of the pointer, to pass to GC.free later.

-Steve
June 17, 2013
On Monday, 17 June 2013 at 17:09:45 UTC, Steven Schveighoffer wrote:
> On Sun, 16 Jun 2013 17:37:15 -0400, Namespace <rswhite4@googlemail.com> wrote:
>
>> But if I call 'destroy' before I call GC.free, it does not work correct.
>
> Destroy puts anything back into it's initial state.  So for anything that is a pointer, destroy will set it to null!  Then calling GC free on null is no good.
>
> What you have to do is save a copy of the pointer, to pass to GC.free later.
>
> -Steve

Ok, thanks.
And you think that my code is working as intended? Or have I overlooked something obvious.
June 18, 2013
> 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

Why the members could not be destroyed? Could you give me an example?
And did you have a link, that explain how I should use TypeInfo to destroy the struct correctly?
June 18, 2013
Here my new version:
----
void Delete(T)(ref T var) if (isAssignable!(T, typeof(null)) && !isStaticArray!T) {
	const bool isPtr = is(T : U*, U);

	static if (isPtr && (is(U == struct) || is(U == class)))
		.destroy(*var);

	static if (is(T : U[], U))
		GC.free(var.ptr);
	else {
		static if (isPtr)
			GC.free(var);
		else
			GC.free(&var);
	}

	var = null;
}
----

That should fulfill all wishes. :)
June 18, 2013
Namespace:

> Here my new version:
> ----
> void Delete(T)(ref T var) if (isAssignable!(T, typeof(null)) && !isStaticArray!T) {
> ...
> ----
>
> That should fulfill all wishes. :)

Its name should start with a lowercase, according to the D style.

Bye,
bearophile
June 18, 2013
On Tuesday, 18 June 2013 at 09:16:05 UTC, bearophile wrote:
> Namespace:
>
>> Here my new version:
>> ----
>> void Delete(T)(ref T var) if (isAssignable!(T, typeof(null)) && !isStaticArray!T) {
>> ...
>> ----
>>
>> That should fulfill all wishes. :)
>
> Its name should start with a lowercase, according to the D style.
>
> Bye,
> bearophile

Maybe, but 'delete' is already a keyword and 'destroy' already exists.
June 18, 2013
Just for you:
http://dpaste.1azy.net/3b46c669

This code works now perfect and works also with forward referenced opaque structures.
June 18, 2013
On Tue, 18 Jun 2013 03:59:37 -0400, Namespace <rswhite4@googlemail.com> wrote:

>> 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
>
> Why the members could not be destroyed? Could you give me an example?
> And did you have a link, that explain how I should use TypeInfo to destroy the struct correctly?

Be careful what hidden members you call, some of them are not what you think they are.

As a guide, you should take a look at the destroy function in object to see the proper method of destroying things.

-Steve
June 18, 2013
> Be careful what hidden members you call, some of them are not what you think they are.
For example?
> As a guide, you should take a look at the destroy function in object to see the proper method of destroying things.
>
> -Steve
June 18, 2013
On Tue, 18 Jun 2013 09:34:44 -0400, Namespace <rswhite4@googlemail.com> wrote:

>> Be careful what hidden members you call, some of them are not what you think they are.
> For example?

For example, __dtor may not be the full destructor, but just the code represented by ~this().  I think this is the case for structs.

I have a bug report somewhere, let me see...

Oops, not my bug, but I commented on it :)  See comment 6 from Kenji:

http://d.puremagic.com/issues/show_bug.cgi?id=5667#c6

-Steve