[Mono-list] passing MonoArray from c#

Robert Jordan robertj at gmx.net
Sat May 2 13:46:43 EDT 2009

fozzie wrote:
>      MonoArray *retLinks = (MonoArray *)mMonoClass->invoke("Pack", args);
>      DataLink *link = (DataLink*)mono_array_get(retLinks, DataLink*, 0);
> Unfortunately the link is NULL. The memory pointed to by retLinks does not
> contain valid DataLink data.

Since mMonoClass->invoke() isn't part of the embedded API, I can't
tell you what the problem is. Is this your method?

> Out of curiosity, do you know what the lifetime of the data returned by
> invoke() is? Is it safe to keep referencing it via pointers after the call
> has finished? How is it freed? 

It's freed by the GC at some time after the stack gets unwound.

If you want to keep MonoObjects alive (eg. put them in static memory
or in your unmanaged objects), you have to mono_gchandle_new() them:

// allocate a handle
gpointer handle = mono_gchandle_new (someMonoObj, FALSE);
MonoObject *someObj = mono_gchandle_get_target (handle);
// use someObj

// give up
mono_gchandle_free (handle);


More information about the Mono-list mailing list