Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
Posted

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.
 
Share this answer
 
Comments
Nish Nishant 24-Jan-11 11:41am    
I am not sure this is the issue. If so his first call to Release would have crashed too. In fact he would probably not have been able to instantiate the object at all.
Dave Kreskowiak 24-Jan-11 18:35pm    
Now that I see that it's a C/C++ question, it's a shot in the dark.
Nish Nishant 25-Jan-11 10:30am    
I wish the OP would get back with more info though.
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;
};

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.
 
Share this answer
 
Comments
Kurt Degiorgio 24-Feb-11 6:28am    
I agree with you, whats strange though is how come it isn't crashing on 32bit systems?
mbue 24-Feb-11 8:08am    
It depends on the kind of objects you use. some objects are marshalled by system rpc and become a zombie state on more releases than necessary. you should everytime call Release() corresponding to AddRef() or QueryInterface(). Have you made the call, you should discard the interface pointer (better you use CComPtr). again: the Release() function is not a normal function - you can call as often as you want. This function is called once to release the usage of that interface.
Kurt Degiorgio 24-Feb-11 10:01am    
makes sense ! Thanks for shading light on that, yes its always better to use smart pointer as things can get messy

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900