pass a delegate to an API as a context pointer?

teo teo.ubuntu at yahoo.com
Tue Jul 3 05:52:33 PDT 2007


Frits van Bommel Wrote:
> >> void test(foo f)
> >> {
> >>     xyz(cast(callback)&handler, cast(void*)&f);
> 
> Those casts shouldn't be necessary.
> (And make sure the C function won't store the pointers somewhere, since 
> the pointer to f will be invalid after test exits since it's a local 
> variable)

The pointer to f is stored. The C-library may call my callback say one hour later or every 5 sec.



> I think it's probably just the mismatching calling convention. The 
> handler function expects the argument in EAX (assuming DMD) while the C 
> function puts it on the stack. This causes the D code to dereference 
> whatever is left in EAX, which will cause a crash unless you're very 
> "lucky".

Good that I'm not that "lucky". Otherwise I would have thought that I've done my job well...

I'll check the calling convention and report back. Right now I cannot test.




More information about the Digitalmars-d mailing list