February 19, 2007 Re: Allocating delegate on heap? | ||||
---|---|---|---|---|
| ||||
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)?
|
February 19, 2007 Re: Allocating delegate on heap? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rick Mann | 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
|
Copyright © 1999-2021 by the D Language Foundation