wrapping a C style delegate

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 26 15:13:48 PDT 2017


On 08/25/2017 05:49 PM, Nicholas Wilson wrote:

 > I was thinking of something along those lines: but what about the
 > lifetime of the passed delegate (the address of a local variable)? How
 > can I ensure that it won't segfault when callback goes out of scope?
 > I could new it with the GC but
 > a) I'd rather have the wrapper be @nogc and
 > b) i'd have to hold a reference to it or pin it because
 > SomeAPIaddCallback will be in a driver somewhere and wouldn't get
 > scanned by the GC.

Good points. I think you have to store a copy of the delegate in 
MyStruct and pass e.g. the array index of it as userData to 
intermediateCallback(). I think a Mallocator-based array would make the 
wrapper nogc.

Ali



More information about the Digitalmars-d-learn mailing list