Object pointer from delegate

Don Clugston dac at nospam.com.au
Mon Aug 28 23:34:53 PDT 2006

Craig Black wrote:
>> (Since the delegate will point to a thunk, it may be possible to 
>> disassemble the thunk to recover the object pointer. Maybe).
> I've not heard about this before.  I only have a fuzzy understanding of what 
> a thunk is.  What is the benefit of using thunks in this context?
> -Craig 

In the current situation, delegates are structs with an objectpointer 
and a function pointer. Calling a delegate dg() = someobj.somefunc()
results in code something like:

mov ebx, dg.objectpointer
call dg.functionpointer  --> calls somefunc.

In D 2.0, delegates will just be function pointers. The equivalent code is

call [dg] --> calls thunk

mov ebx, someobj
jmp somefunc

A major advantage is that you can pass a delegate to a C function that 
expects a function pointer (eg, the WindowsAPI). This is how WTL works 
in the C++ world. The thunk code needs to be created at run time; 
normally, it's created on the stack.

More information about the Digitalmars-d-learn mailing list