不确定为什么会这样。由于每个接口都支持IUnkown,因此 Release()应该释放COM对象。 COM对象只创建了一次 。 我是C#的新手,所以我很感激一些帮助。I have a VC++ client that creates a COM object that is implemented in C#.The problem is the COM object doesn''t get released, so I get a memory leak.I have no problems creating the COM object and getting a pointer to therequired interface (IATImportFilter) in the client:COleDispatchDriver* resultInterface = new COleDispatchDriver;if ( resultInterface == NULL ) return FALSE;COleException oOleException;BOOL success = resultInterface->CreateDispatch( progID, &oOleException );IUnknown *p;if ( resultInterface->m_lpDispatch->QueryInterface( *pRequiredInterface,(void**)&p ) != S_OK )where prodID ("SI.ValidatorImport") is the name of COM object.My C# COM object class is declared like this:[ClassInterface(ClassInterfaceType.None)][ProgId("SI.ValidatorImport")]public class ValidatorImport : IATImportFilter{The object is being released in the client like this:((IUnknown*) pIF)->Release();pIF = NULL;The call to Release() on the interface does not actually destroy the object.It doesn''t call the destructor of my COM object. I tried putting abreakpoint in there and even added a MessageBox() call but the program justdoesn''t go in there.Not sure why this happening. Since every interface supports IUnkown,Release() should release the COM object. The COM object is only createdonce.I''m new to C# so I would appreciate some help.推荐答案>对接口上的Release()的调用实际上并没有销毁该对象。>The call to Release() on the interface does not actually destroy the object.它没有调用我的COM的析构函数对象。It doesn''t call the destructor of my COM object. 因为它是一个托管对象,它最终将被 垃圾收集器清理。 Mattias - Mattias Sj?gren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com 请回复到新闻组。Since it''s a managed object it will eventually be cleaned up by thegarbage collector.Mattias--Mattias Sj?gren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.comPlease reply only to the newsgroup.在消息< jr ******************** @ biscit.net>中, techie < te **** @ nospam.biz>写In message <jr********************@biscit.net>, techie<te****@nospam.biz> writes对接口上的Release()的调用实际上并不会破坏对象。它不会调用我的COM对象的析构函数。我尝试在那里放一个断点,甚至添加了一个MessageBox()调用,但程序只是不会进入那里。 不知道为什么会发生这种情况。由于每个接口都支持IUnkown,因此Release()应该释放COM对象。 COM对象只创建一次。The call to Release() on the interface does not actually destroy the object.It doesn''t call the destructor of my COM object. I tried putting abreakpoint in there and even added a MessageBox() call but the program justdoesn''t go in there.Not sure why this happening. Since every interface supports IUnkown,Release() should release the COM object. The COM object is only createdonce. 据我所知,当你调用Release()时,你会在 COM可调用包装器,而不是对象本身。 COM可调用包装器 引用计数与您期望的COM对象相同。当所有 引用都被释放后,它会释放自己对您在C#中写入的对象的引用,然后可以进行垃圾回收。这将在某个时刻发生,但不一定在你预期的时候发生。所以,你要没有得到确定性的C#对象的最终确定。你*可能*获得包装纸的DF ,我看到的文件并不清楚它是否已经立即销毁你的b $ b已经固定在一个非GC的' 堆上,再次是AIUI,所以可能它会立即在 正常COM时尚中被破坏。 你可能没有内存泄漏,你很可能有一个 未引用的对象等待GC'。 - Steve WalkerAs I understand it, when you call Release(), you''re calling it on aCOM-callable wrapper, not on the object itself. The COM callable wrapperreference counts as you would expect a COM object to. When allreferences are released, it releases its own reference to the object youwrote in C#, which is then available for garbage collection. This willhappen at some point, but not necessarily when you expect it to. So, youdon''t get deterministic finalisation of your C# object. You *may* get DFof the wrapper, the documentation I''ve seen isn''t clear on whether it isdestroyed immediately you are done with it It''s pinned on a non GC''dheap, again AIUI, so presumably it is destroyed immediately in thenormal COM fashion.You probably don''t have a memory leak, you more than likely have anunreferenced object sat waiting to be GC''d.--Steve Walker" Steve Walker" < ST *** @ otolith.demon.co.uk>在消息中写道 新闻:pI ************** @ otolith.demon.co.uk ..."Steve Walker" <st***@otolith.demon.co.uk> wrote in messagenews:pI**************@otolith.demon.co.uk...你可能不喜欢没有内存泄漏,你很可能有一个未引用的对象等待GC'。 You probably don''t have a memory leak, you more than likely have an unreferenced object sat waiting to be GC''d. 有没有我可以强制GC?也许如果我的课程也实现了IDisposable 我可以从我的客户那里打电话给我?Is there any I can force GC? Maybe if my class also implements IDisposableI can call dispose from my client? 这篇关于C#COM对象未被释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 21:41