COM方法发布:应用程序崩溃 [英] COM Method Release: Application Crashing

查看:136
本文介绍了COM方法发布:应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用Release()方法时遇到一个问题.

首次调用Release方法时,应用程序
工作正常,但如果第二次调用则崩溃.

它仅在64位系统中发生.
相同的代码可以在32位系统中正常运行.

如果有人对此类问题有任何线索,请帮助...

I am having one problem in calling Release() method.

When the Release method is called for the first time then the application
is working fine but if called for the second time it''s crashing.

It''s happenning in 64-bit system only.
The same code runs fine in 32 bit system.

If anyone have some clue about this kind of problem please help...

推荐答案

这可能是因为您尝试运行64位和32位代码的中间原因在相同的过程中.确保您的应用未针对AnyCPU或x64进行编译,并且您的COM组件仅为32位.重新编译您的应用程序,使其仅作为x86的目标,就可以了.
This is probably because you have a mid of 64 and 32-bit code trying to run in the same process. Make sure you''re app is not compiled for AnyCPU or x64 and that your COM componet is 32-bit only. Recompile your app as targing x86 only and you should be fine.


我认为您不了解COM概念.
COM对象的简单实现:
I think you dont understand the COM concept.
A simple implementation of a COM object:
class iUnknownImpl : public IUnknown
{
public: // IUnknown
  virtual HRESULT __stdcall  QueryInterface(REFIID riid,void** ppv)
    { return IID_IUnknown==riid?(*(IUnknown**)ppv=this,AddRef(),S_OK):E_NOINTERFACE; }
  virtual unsigned long __stdcall  AddRef()
    { return InterlockedIncrement(&_ref); }
  virtual unsigned long __stdcall  Release()
    { if(InterlockedDecrement(&_ref)) return _ref; delete this; return 0; }
  iUnknownImpl(){ _ref=1; }
protected:
  virtual ~iUnknownImpl(){ ASSERT(0==_ref); }
private:
  long  _ref;
};


您会看到是否要保留接口指针-您必须调用AddRef().同时其他线程可以释放接口.如果您不再需要该接口,则必须调用Release()(一次).如果要将接口转换为另一个接口,则必须调用QueryInterface().请记住:每次AddRef()QueryInterface()调用都需要一个Release.
释放指针后,该对象将被破坏.
AddRef的数量多于Release的数量,会产生内存泄漏.比AddRef发行更多的版本会使您的应用崩溃.
问候.


You see if you want to hold an interface pointer - you have to call AddRef(). Other threads can meanwhile release the interface. If you dont need the interface anymore you must call Release() (once). If you want to cast an interface to another you have to call QueryInterface(). Remember: every call of AddRef() and QueryInterface() needs one Release.
After Release your pointer is invalid the object can be destroyed.
More AddRef''s than Release''s generate memory leaks. More Release''s than AddRef''s makes your app crash.
Regards.


这篇关于COM方法发布:应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆