Bug 9738 - Calling an unmanaged thunk creates a NullReferenceException
Summary: Calling an unmanaged thunk creates a NullReferenceException
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2013-01-23 12:25 UTC by Tom Mason
Modified: 2017-08-17 17:24 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Tom Mason 2013-01-23 12:25:51 UTC
I'm experiencing a problem calling an unmanaged thunk into managed code.
The first time I call the thunk, it works perfectly.  The second time, my
debugger receives a seg fault and then mono prints out that I got a
NulllReferenceException.  Calling mono_runtime_invoke on the managed method
also works perfectly, every time I call it.  I want to get thunks working
though, since performance is a concern.

I init mono, register an internal call, and then run a simple C# assembly
that makes the internal call to give my C++ code a MonoObject* for the
managed class instance.  That part works perfectly, and looks like this:

g_mobj = mobj;
MonoClass *mclass = mono_object_get_class(mobj);
g_mmethod = mono_class_get_method_from_name(mclass, "Add", 2);
g_mthunk = (MonoAddFunc)mono_method_get_unmanaged_thunk(g_mmethod);

Where MonoAddFunc is defined like this:
typedef float (*MonoAddFunc)(MonoObject*, float, float);

This is the C# Add method:

public float Add(float a, float b) {
        return a + b;

This is the code for calling Add using mono_runtime_invoke (works

float a = 0.00001f, b = 0.00002f;
void *args[] = { &a, &b };
for(int i = 0; i < NUM_CALLS; ++i) {
        MonoObject *result = mono_runtime_invoke(g_mmethod, g_mobj, args, NULL);
        total += *(float*)mono_object_unbox(result);

This the code for calling Add with the thunk (fails on 2nd time through
loop, i == 1):

for(int i = 0; i < NUM_CALLS; ++i) {
        total += g_mthunk(g_mobj, 0.00001f, 0.00002f);

When I compiled a release build, then calling the thunk failed the first time.

I wrote another test app where I created the thunk the same way, and it
works fine, but the other method I called in that app had no parameters.
Comment 1 Rodrigo Kumpera 2017-07-14 23:47:02 UTC
Coul you provide a complete test case that shows your issue so we can investigate?
Comment 2 Ludovic Henry 2017-08-17 17:24:44 UTC
Please reopen whenever you provide a reproduction case so we can have a look. Thank you.