On Saturday, 28 February 2015 at 20:49:22 UTC, Marc Schütz wrote:
I encountered an ugly problem. Actually, I had already run into
it in my first proposal, but Steven Schveighoffer just posted
about it here, which made me aware again:
http://forum.dlang.org/thread/[email protected]#post-mcqk4s:246qb:241:40digitalmars.com
class T {
void doSomething() scope;
}
struct S {
RC!T t;
}
void main() {
auto s = S(RC!T()); // `s.t`'s refcount is 1
foo(s, s.t); // borrowing, no refcount changes
}
void foo(ref S s, scope T t) {
s.t = RC!T(); // drops the old `s.t`
t.doSomething(); // oops, `t` is gone
}
One quick thing. I suggest a solution here:
http://forum.dlang.org/post/[email protected]
You do the checking and adding in the called function, not the
caller. The algorithm:
1. Keep a compile-time refcount per function. Does the parameter
get released, i.e. does the refcount ever go below 1? If not,
stop.
2. Can the parameter contain (as a member) a reference to a
refcounted struct of the types of any of the other parameters? If
not, stop.
3. Okay, you need to preserve the reference. Add a call to opAdd
at the beginning and one to opRelease at the end of the function.
Done.