尽管我使用 Marshal.FinalReleaseComObject,但不取消订阅 COMObject 事件会导致内存泄漏吗?

发布于 2025-01-03 23:38:24 字数 828 浏览 0 评论 0原文

尽管我使用 Marshal.FinalReleaseComObject,但不取消订阅 COMObject 事件会导致内存泄漏吗?

我在我的类中定义了新成员 COMObject

protected COMObject.Call call_ = null;

该类具有我订阅的以下事件处理程序

call_.ActionA += new COMObject.AEventHandler(AEvent);
call_.ActionB += new COMObject.BEventHandler(BEvent);
call_.Destructed += new COMObject.DestructedEventHandler(CallDestructedEvent);

当调用 Destructed 事件时,我执行 Marshal.FinalReleaseComObject

Marshal.FinalReleaseComObject(call_) 

但不取消订阅事件

call_.ActionA -= new COMObject.AEventHandler(AEvent);
call_.ActionB -= new COMObject.BEventHandler(BEvent);
call_.Destructed -= new COMObject.DestructedEventHandler(CallDestructedEvent);

这是否会导致内存泄漏?或者GC会处理它? 你能将你的答案链接到一些 MSDN 或文章吗?

谢谢!多尔。

Do not unsubscribe from COMObject events can cause memory leak although I use Marshal.FinalReleaseComObject?

I have defined new member in my class that is COMObject

protected COMObject.Call call_ = null;

This class has the following event handlers that I subscribed to

call_.ActionA += new COMObject.AEventHandler(AEvent);
call_.ActionB += new COMObject.BEventHandler(BEvent);
call_.Destructed += new COMObject.DestructedEventHandler(CallDestructedEvent);

When the Destructed event is called I do Marshal.FinalReleaseComObject

Marshal.FinalReleaseComObject(call_) 

but not unsubscribe from the events

call_.ActionA -= new COMObject.AEventHandler(AEvent);
call_.ActionB -= new COMObject.BEventHandler(BEvent);
call_.Destructed -= new COMObject.DestructedEventHandler(CallDestructedEvent);

Is this can cause memory leak? or that the GC will handle it?
Can you link your answer to some MSDN or article?

Thanks! Dor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

阳光的暖冬 2025-01-10 23:38:24

您是否在发布之前尝试取消订阅它们,看看是否有助于内存泄漏?
你有一个被破坏的处理程序,你可以在那里做。

您是否曾经调用过 COM 对象的任何属性?如果您这样做了,您是否已释放他们也正确吗?
我认为即使您在父对象上调用 FinalReleaseComObject,如果您也没有释放子对象,那么即使没有对父对象的引用,仍然可能有对挂起的子对象的引用。

Have you tried unsubscribing them before doing the release to see if it helps the memory leak?
You've got a destructed handler, you could do it in there.

Do you ever call any of the properties of the COM object, and if you do, have you released them properly too?
I think that even if you call FinalReleaseComObject on the parent object, if you haven't released the child objects too then even though there are no reference to the parent there may still be references to the children hanging about.

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