C++/CLI 在多个 gcroot 托管对象之间交换对象引用
嗨,不幸的是我在整个垃圾收集方面不太擅长。现在我不确定如果我执行以下操作是否会遇到麻烦:
// unmanaged Class
class CUnmagedClass : public CCmdTarget
{
auto_gcroot<Dictionary<int, System::String^>^> m_dict;
auto_gcroot<SomeManagedClass^> m_managedClass;
// create first manage object in one gcroot
CUnmagedClass()
:dict(gcnew Dictionary<LANGID, System::String^>())
{}
// do something with the dictionary
void AddData(int key, String^ value)
{
this->m_dict->Add(key, value);
}
// Method that could be called multiple times
void DoOtherThings(Data^ data)
{
// create a new object on every method call
// old object can be garbage collected
this->m_managedClass = gcnew SomeManagedClass(data);
// assign a reference to the dictionary from the other gcroot
this->m_managedClass->DictProp = this->m_dict;
this->m_managedClass->DoSomething();
}
}
所以我的问题是,如果我覆盖 this->m_managementClass
和旧 中的值SomeManagedClass
实例被垃圾收集。系统是否也尝试收集 this->m_managementClass->DictProp
中的对象,因为它不再连接到第二个 gcroot,或者垃圾收集器是否足够聪明,知道还存在引用在另一个 gcroot
中?
Hi unfortunately I'm not so good in the whole garbage collection thing. Now I am unsure if I'm going to get into trouble if I'm doing the following:
// unmanaged Class
class CUnmagedClass : public CCmdTarget
{
auto_gcroot<Dictionary<int, System::String^>^> m_dict;
auto_gcroot<SomeManagedClass^> m_managedClass;
// create first manage object in one gcroot
CUnmagedClass()
:dict(gcnew Dictionary<LANGID, System::String^>())
{}
// do something with the dictionary
void AddData(int key, String^ value)
{
this->m_dict->Add(key, value);
}
// Method that could be called multiple times
void DoOtherThings(Data^ data)
{
// create a new object on every method call
// old object can be garbage collected
this->m_managedClass = gcnew SomeManagedClass(data);
// assign a reference to the dictionary from the other gcroot
this->m_managedClass->DictProp = this->m_dict;
this->m_managedClass->DoSomething();
}
}
So my question is, if I overwrite the value in this->m_managedClass
and the older SomeManagedClass
instance is garbage collected. Does the system try to collect the object in this->m_managedClass->DictProp
too because it's not connected to the second gcroot anymore or is the garbage collector clever enough to know that there is a reference left in the other gcroot
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
系统也不会尝试收集
this->m_managementClass->DictProp
,因为它足够聪明,知道该对象被另一个gcroot
引用。gcroot
如此聪明的原因是它包装了System::Runtime::InteropServices::GCHandle
,这是垃圾收集堆中的句柄。 GCHandle 类允许非托管对象存储有效的托管引用。此外,即使垃圾收集器移动了托管对象,GCHandle
引用也会更新以指向其新位置。The system will not try to collect
this->m_managedClass->DictProp
too because it is clever enough to know that this object is referenced by the othergcroot
.The reason the
gcroot
is this clever is that it wrapsSystem::Runtime::InteropServices::GCHandle
, which is a handle into the garbage collected heap. TheGCHandle
class is what allows your unmanaged object to store what is effectively a managed reference. Also, even if your managed object is moved by the garbage collector, theGCHandle
reference will be updated to point to its new location.