C++/CLI 在多个 gcroot 托管对象之间交换对象引用

发布于 2024-09-08 00:41:16 字数 1242 浏览 3 评论 0原文

嗨,不幸的是我在整个垃圾收集方面不太擅长。现在我不确定如果我执行以下操作是否会遇到麻烦:

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在你怀里撒娇 2024-09-15 00:41:16

系统也不会尝试收集 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 other gcroot.

The reason the gcroot is this clever is that it wraps System::Runtime::InteropServices::GCHandle, which is a handle into the garbage collected heap. The GCHandle 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, the GCHandle reference will be updated to point to its new location.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文