On Sunday, 27 October 2013 at 21:02:01 UTC, Rene Zwanenburg wrote:
On Thursday, 24 October 2013 at 16:46:37 UTC, Ali Çehreli wrote:
Technically, it is a problem with FooWrapper. Regardless of whether RefCounted's behavior, by default, structs in D are freely copyable and movable value types. The compiler can do those things as it sees fit.

Since FooWrapper owns a resource, it must also define the post-blit to make a copy of Foo.

(As an aside, dmd at git head does not make such a copy.)

Ali

Thanks, that's indeed an oversight on my part. Still, I think there's something strange going on.

The problem is that the FooWrapper destructor is called twice, one time with a garbage foo pointer. The example prints:
Destroying Foo 40CED748
Destroying Foo 4002DFF0
There is only one Foo instance created. A null pointer would make sense, but where's that second pointer coming from?

This is indeed strange. I don't have access to my development
platform, but it *could* be an (older) emplace bug. Do you
reproduce with head? I'll investigate further as soon as I can.
This is not normal behavior.

Also, keep in mind that DMD *is* allowed to destroy the same
object several times. YOur destructor should look like this:
~this()
{
     if (foo)
         destroyFoo(foo);
     foo = null;
}

This doesn't explain what you are seeing, but keep it in mind.

Reply via email to