www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Go: A new system programing language

reply Mike Hearn <mike plan99.net> writes:
 Mixing D's gc world with manually managed memory isn't 
 hard, as long as  the following rules are followed:
 
 1. don't allocate in one language and expect to free in another
 2. keep a 'root' to all gc allocated data in the D side of the fence 
 (otherwise it may get collected)

Yes it's the second that's the tough part. For instance consider the case of passing a callback (delegate) to the RPC system written in c++. How do you keep the associated data rooted without causing leaks? You'd need to remember to manually add it to the gc roots when the callback object is created and then unroot them when it's invoked. So this needs some kind of glue/binding system. I'm not saying it's impossible or even hard. Just that I've seen such things done before and they were non-trivial.
Nov 18 2009
parent Walter Bright <newshound1 digitalmars.com> writes:
Mike Hearn wrote:
 Mixing D's gc world with manually managed memory isn't hard, as
 long as  the following rules are followed:
 
 1. don't allocate in one language and expect to free in another 2.
 keep a 'root' to all gc allocated data in the D side of the fence 
 (otherwise it may get collected)

Yes it's the second that's the tough part. For instance consider the case of passing a callback (delegate) to the RPC system written in c++. How do you keep the associated data rooted without causing leaks? You'd need to remember to manually add it to the gc roots when the callback object is created and then unroot them when it's invoked. So this needs some kind of glue/binding system. I'm not saying it's impossible or even hard. Just that I've seen such things done before and they were non-trivial.

Most of the time, nothing needs to be done because the reference is on the parameter stack that calls the C function. For the callback object, manually adding/removing the root (there are calls to the gc to do this) shouldn't be any more onerous than manually managing memory for it, which is done in C/C++ anyway.
Nov 18 2009