Bug in RefCounted?

Ali Çehreli acehreli at yahoo.com
Thu Oct 24 09:46:37 PDT 2013


On 10/24/2013 07:58 AM, Rene Zwanenburg wrote:
> I'm writing a D wrapper for a C library. I was planning to use
> RefCounted structs to control the lifetime of objects created by this
> library. Please check the following example:
>
> http://dpaste.dzfl.pl/b49962bf
>
> Foo would be an opaque struct. createFoo() and destroyFoo() would be
> implemented in the C library (I know I have to declare them extern(C),
> this is just an example).
>
> As you can see, that code prints 'Destroying Foo' twice, with different
> pointers. I expected destroyFoo to be called only once, on the instance
> created by createFoo(), when the Bar instance goes out of scope. Current
> behaviour causes an invalid pointer to be passed to destroyFoo(). Is
> this a bug in RefCounted or am I doing something wrong?

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



More information about the Digitalmars-d-learn mailing list