[Mono-dev] Marshal.GetFunctionPointerForDelegate and non-mono threads

sebastian sebastian at palladiumconsulting.com
Mon Jan 28 18:42:33 UTC 2013


We run a program under mono which uses a 3rd party C++ library. Mono is
responsible for running the application, that is, we are not using the
mono_embed API, but rather just PInvoke to talk to the C++ library.

This library has some callbacks which we subscribe to using
Marhsal.GetFunctionPointerForDelegate. However we get exotic concurrency
problems (seg faults, inexplicable stacktraces) when we use it. We only get
these errors when the callback is made from a thread which was not started
by us.

I know that when embedding mono, i.e. with C++ in the driver's seat,
threads must be registered with mono using mono_thread_attach. However that
would be a funny thing for us to do, since we're not launching mono
ourselves and would have to do some exploration to find all the right
pointers.

Does the code in GetFunctionPointerForDelegate emit a managed wrapper that
takes care of this detail? Once we are called back on this foreign thread,
there's no telling what or how much .NET code will run on it, and it
presumably needs to be properly registered with the garbage collector. I
looked at the code in mono_marshal_get_managed_wrapper and didn't see
anything obviously related to threading, but I imagine it'd be taken care
of at a lower level in any case.

We could easily be convinced the bugs we saw were GC or threading related,
as they could only be explained by corruption of things that shouldn't be
corruptible, like reflection and array bounds.

Or is there additional code or attributes we should be using to ensure
correct operation?

Thanks,
Sebastian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20130128/d2dda592/attachment.html>


More information about the Mono-devel-list mailing list