[Mono-dev] garbage collection and the embedding API

Andreas Färber andreas.faerber at web.de
Wed Oct 31 14:29:46 EDT 2007


Am 31.10.2007 um 19:20 schrieb Sebastian Good:

> We are writing an extensive set of libraries in .NET and would like  
> to expose them to a C++ application. We are using the embedding  
> API, exposing thin wrappers of MonoObject* to the C++ program. They  
> will call our functions routinely. We have a few questions:
>
> What are the lifetime rules for a MonoObject* created with  
> mono_object_new? Is there a corresponding free that needs to be  
> called? I understand that within the JIT's heaps, garbage  
> collection takes care of such issues, but when a CIL pointer  
> "escapes" into the C++ world, I  am having trouble finding  
> documentation describing what to do. I understand it is possible to  
> "pin" these MonoObject*s so they don't move around, but I assume  
> this is unnecessary until a Compacting GC is actually released.
>
> Experimentation suggests that one can indeed be careless with these  
> pointers and not suffer a memory leak, but I'd like to have a firm  
> understanding.
>
> From the discussion at http://www.mono-project.com/ 
> Mono:Runtime#Garbage_Collection it seems like  the unmanaged C++  
> heap is not scanned but it seems to me like it is. Does the GC  
> linked in with mono replace the default new() or malloc operators?

Look into Mono's gchandle functions, pinning the objects is only an  
option. As long as you own a handle to the object it shouldn't be  
collected, whether pinned or not. Since you don't know whether there  
are managed references to the object, you don't free it yourself.

Andreas



More information about the Mono-devel-list mailing list