0
嗨,不幸的是我在整个垃圾收集事情上并不那么优秀。现在我不能确定,如果我要惹上麻烦,如果我做了以下内容:多个gcrooted托管对象之间的C++/CLI交换对象引用
// 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_managedClass
和值SomeManagedClass
实例是垃圾回收旧。系统是否尝试收集this->m_managedClass->DictProp
中的对象,因为它不再与第二个gcroot连接,或者垃圾收集器足够聪明以知道在其他gcroot
中有剩余的引用?