简体   繁体   English

嵌入式Mono:在C ++中保留对C#对象的引用

[英]Embedded Mono: Keeping references to C# objects in C++

I'm working on embedding mono into an application I'm creating, and I haven't gotten super far, but one of the things I can't seem to find is how to tell mono when I'm using an object and done with an object. 我正在将mono嵌入到正在创建的应用程序中,但还没有走得太远,但是我似乎找不到的一件事是当我使用对象并完成操作时如何告诉mono。与一个对象。

I want to keep a reference to a C# object to call methods on until the lifetime of it's parallel object in C++ is over, at which point, I want to tell mono that the C# object is safe to collect. 我想保留对C#对象的引用以调用方法,直到它在C ++中的并行对象的生命周期结束为止,此时,我想告诉Mono,C#对象可以安全地收集。

How is this accomplished? 如何完成的?

It appears that what I'm looking for is mono_gchandle_new , and hold onto the handle, not the MonoObject*, and use mono_gchandle_get_target when I need it. 似乎我正在寻找的是mono_gchandle_new ,并握住了句柄,而不是MonoObject *,并在需要时使用mono_gchandle_get_target。

mono_gchandle_new allows you to pin when creating the handle, but is it possible to pin after the fact? mono_gchandle_new允许您在创建句柄时进行锁定,但是事实发生后是否有可能进行锁定?

One thing to keep in mind when using mono_gchandle_new() that I encountered.. it will keep only the C# object you referenced in memory, but if that object allocates other objects those are still subject to the garbage collection routines. 使用我遇到的mono_gchandle_new()时要记住的一件事..它将仅将您引用的C#对象保留在内存中,但是如果该对象分配其他对象,则这些对象仍将接受垃圾回收例程。 The fact that a object you have a handle for, can have it's sub-objects freed on you has caused me quite a bit of trouble. 您拥有处理对象的对象可以将其子对象释放给您的事实给我造成了很多麻烦。

I'm currently digging through the mono GC system to see if I can fix it so it will treat those objects as root objects. 我目前正在研究mono GC系统,看是否可以修复它,以便它将那些对象视为根对象。

If you have few enough objects (<4096), you can use mono_gc_register_root()... we can have thousands of objects, so this is no good for our uses. 如果对象不足(<4096),则可以使用mono_gc_register_root()...我们可以拥有数千个对象,因此这对我们的使用不利。

UPDATE: So I was incorrect about this, we had hooked into the mono object allocation system and we weren't passing through the "atomic" variable correctly onto the GC allocation functions. 更新:所以我对此是不对的,我们陷入了单对象分配系统中,并且没有将“ atomic”变量正确地传递给GC分配函数。 "Atomic" means something different to the GC, it doesn't have anything to do with concurrent access, it actually means the memory being allocated references other objects (atomic=0) or not (atomic=1). “原子”是指与GC不同的东西,它与并发访问没有任何关系,实际上是指分配的内存引用了其他对象(原子= 0)或没有其他对象(原子= 1)。

Use System::GCHandle::Alloc and call ToIntPtr on the result to get a token and protect the object from collection. 使用System::GCHandle::Alloc并在结果上调用ToIntPtr以获取令牌并保护该对象免于收集。 Call ToPointer() and store as a void* . 调用ToPointer()并存储为void*

Use System::GCHandle::FromIntPtr when you need to map the token to the object, or release it so it is again eligible for collection. 需要将令牌映射到对象或释放它时,请使用System::GCHandle::FromIntPtr ,以便再次有资格进行收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM