On 2/24/15 2:40 PM, Andrei Alexandrescu wrote:
I modified Walter's sample code to this:
http://dpaste.dzfl.pl/f3d854feede9. It uses malloc for both the array
and the reference count, and also uses @trusted minimally. I inserted
assert()s here and there to clarify the workings. Nothing big except for
the careful use of @trusted.
I'll use this as a basis of some exegesis.
1. The code is a bit more complicated than it should. Overall this is
not a biggie; regular D users are not supposed to write reference
counted slices casually. But I was bummed that e.g. I found no way to
call emplace() @safe-ly.
I have no problem with underlying complexity of primitive types. Using
Objective-C objects is just fine without understanding the implementation.
Note, you need to GC.addRange all the elements if the type has
references, or else you cannot have GC pointers in the array. For
example, an array of class references could potentially result in those
references being collected before the array is gone.
Ironically, if those elements are references, but are reference counted
references, then you wouldn't need to addRange. An interesting problem...
2. Michel's point (https://issues.dlang.org/show_bug.cgi?id=14221)
reveals the largest issue with RC/GC integration. We need to find a fix
for it if we want to have the GC lift cycles.
I think a system of making sure a piece of data is always destroyed in
the same thread it was created (unless of course, the thread is gone, in
which case it doesn't matter), should be fine.
-Steve