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
中有剩餘的引用?