
Writing a D Wrapper for a C Library - ingve
https://dlang.org/blog/2019/02/10/writing-a-d-wrapper-for-a-c-library/
======
13415
As someone who never had a problem with it, I would be interested in learning
what drew you away from Ada to D.

------
dbaupp
It seems like this is an unsafe and also leaky way of wrapping the API?
Copying an URIWithoutFinalise seems to allocate a new C pointer and never free
it, and it seems the URI type can be copied without creating a new pointer,
leading to double frees! Maybe I'm misunderstanding D here, but shouldn't the
new/copy/free all happen in a single object similar to the C++ rule-of-three?

If that's not possible, isn't it typical to give the pointers an "identity"
(store them in a class) to provide a GC-language with safety when wrapping a C
API that seems to use a more affine/linear ownership system?

It seems to be undermining D's claims of safety if the "official" (widely
published on the official blog) way to wrap C APIs is unsafe.

(Plus, at least the example code listings for the mixins, failure (returning
NULL) of the C calls isn't checked.)

~~~
ben-schaaf
Yes, you are reading the D incorrectly. `URIWithoutFinalise` has no post-blit
constructor (D does not have copy constructors) and thus does nothing on copy.
It does have a method called `dup` that returns a `URI`(WithFinalization), but
this is not called on copy.

`URI` cannot be copied because the post-blit constructor is disabled. The
default constructor is also disabled, so a "null" URI couldn't be created
either.

You'll get no argument from me about not checking for failure though. Some
asserts at least would be a good idea there.

~~~
dbaupp
Oh, awesome! Thanks!

