www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Re: Allocating delegate on heap?

reply Rick Mann <rmann-d-lang latencyzero.com> writes:
Kirk McDonald Wrote:

 However, there's a very important detail which you should not overlook: 
 You are passing a pointer to GC-controlled data to a C library. The D 
 garbage collector doesn't scan the C library for references, so you will 
 need to keep a reference to any structs you pass to Carbon in your D 
 code. Otherwise, the GC will feel free to collect them when you least 
 expect it. This can be done with an AA:

Yes, thank you, I was aware of that. As it turns out, there's other stuff I need to do with my delegate that I can't do (namely, treat the .ptr portion of it as a pointer to a class derived from some interface Foo), so I ended up having to create a little class wrapper. I add the class to the GC's root with std.gc.addRoot(). This should work, right (rather than storing it in an AA)?
Feb 19 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Rick Mann wrote:
 Kirk McDonald Wrote:
 
 However, there's a very important detail which you should not overlook: 
 You are passing a pointer to GC-controlled data to a C library. The D 
 garbage collector doesn't scan the C library for references, so you will 
 need to keep a reference to any structs you pass to Carbon in your D 
 code. Otherwise, the GC will feel free to collect them when you least 
 expect it. This can be done with an AA:

Yes, thank you, I was aware of that. As it turns out, there's other stuff I need to do with my delegate that I can't do (namely, treat the .ptr portion of it as a pointer to a class derived from some interface Foo), so I ended up having to create a little class wrapper. I add the class to the GC's root with std.gc.addRoot(). This should work, right (rather than storing it in an AA)?

That's going to tie you to a particular gc implementation, though. So I think the practice should be avoided unless you have a compelling reason to do it that way. --bb
Feb 19 2007