[Mono-list] NullReferenceException when calling thunk

Robert Jordan robertj at gmx.net
Wed Jan 23 18:23:22 UTC 2013


Hi Tom,

On 23.01.2013 18:16, tomason wrote:
> That's interesting that the thunks are a seldom-used feature.  The
> documentation says that they're more efficient AND they're way easier to
> use than mono_runtime_invoke.  Is the latter really more popular or is
> there a 3rd way that I don't know about yet?

The 3rd way revolves around calling back a delegate registered
from the managed world via p/invoke:

// C#

delegate float AddMethod(float a, float b);

class Test
{
	[DllImport(...)]
	public static extern
	void RegisterAddCallback(AddMethod floatDelegate);
}

// C/C++

typedef float (*AddMethod)(float, float);
AddMethod managedAddMethod;

void
RegisterAddCallback(AddMethod delegate)
{
	managedAddMethod = delegate;
}

static
float Add(float a, float b)
{
	assert(managedAddMethod);
	return managedAddMethod(a, b);
}


Since this approach is requiring quite a bit of bookkeeping,
it only makes sense to use it for a few methods (or at large scale
using some kind of code generation).

Robert


>
> Thanks!
> -Tom
>
>
> On Wed, Jan 23, 2013 at 2:57 AM, Robert Jordan [via Mono] <
> ml-node+s1490590n4658237h75 at n4.nabble.com> wrote:
>
>> Hi,
>>
>> On 23.01.2013 06:43, tomason wrote:
>>> Where MonoAddFunc is defined like this:
>>> typedef float (*MonoAddFunc)(MonoObject*, float, float);
>>
>> You you're testing under Windows, the typedef must be stdcall:
>>
>> typedef float (__stdcall *MonoAddFunc)(MonoObject*, float, float);
>>
>>
>> A note on thunks: this is a seldom used feature, so odds are
>> that the code got a little bitrotten. Please file a bug if
>> you can rule out the stdcall issue, and I'll take a look
>> at it.
>>
>> Robert
>>
>>
>> _______________________________________________
>> Mono-list maillist  -  [hidden email]<http://user/SendEmail.jtp?type=node&node=4658237&i=0>
>> http://lists.ximian.com/mailman/listinfo/mono-list
>>
>>
>> ------------------------------
>>   If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://mono.1490590.n4.nabble.com/NullReferenceException-when-calling-thunk-tp4658230p4658237.html
>>   To unsubscribe from NullReferenceException when calling thunk, click here<http://mono.1490590.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4658230&code=dG9tQGNyZWFteXNvZnQuY29tfDQ2NTgyMzB8LTE3MTUxMzM0Nzg=>
>> .
>> NAML<http://mono.1490590.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
>
>
> --
> View this message in context: http://mono.1490590.n4.nabble.com/NullReferenceException-when-calling-thunk-tp4658230p4658243.html
> Sent from the Mono - General mailing list archive at Nabble.com.
>
>
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>




More information about the Mono-list mailing list