# Runtime Error (atlbase.h)

## Recommended Posts

guddi1    122
Hi I've been working on an application for so long now. Now that I have gotten rid of all compiler errors, I have a runtime error. It is as follows: Microsoft Visual C++ Debug Library Debug Assertion Failed! Program: ... File: C:\program files\microsoft platform sdk\include\atl\atlbase.h Line: 868 Expression: p!=0 For information on how your program can cause an assertion failure, see the visual C++ documentation on asserts. (Press Retry to debug the application) This error is being caused by the following statement in my code: HRESULT hr = m_spWB->QueryInterface(IID_IConnectionPointContainer, (void**)&m_spCPC); When I click retry, a breakpoint is created by the application in the following code of atlbase.h: //The assert on operator& usually indicates a bug. If this is really //what is needed, however, take the address of the p member explicitly. T** operator&() { ATLASSERT(p==NULL); return &p; } _NoAddRefReleaseOnCComPtr<T>* operator->() const { ATLASSERT(p!=NULL); return (_NoAddRefReleaseOnCComPtr<T>*)p; } T* operator=(T* lp) { return (T*)AtlComPtrAssign((IUnknown**)&p, lp); } The breakpoint points to line 10 of the code above, where is says ATLASSERT(p!=NULL); Can someone please help me understand why this error is showing up and how I can fix it. Thank you.

##### Share on other sites
mfawcett    373
HRESULT hr = m_spWB->QueryInterface(IID_IConnectionPointContainer,(void**)&m_spCPC);

C++ COM classes overload the address of operator to assert if the internal pointer is not NULL. You need to Release before passing in the address of your COM pointer.

As to the "why is it that way"...if it did not assert, it would be allowing memory to leak. Consider (where smart_ptr does NOT overload the address of operator):

void alloc_class(SomeClass **out){   *out = new SomeClass;}smart_ptr<SomeClass> temp;alloc_class(&temp);alloc_class(&temp); // Leaked one SomeClass

Without the assert I would have to remember to Release before calling alloc_class again to not leak memory there.

##### Share on other sites
guddi1    122
You mean I would have to call Release on the m_spWB com object. This object is actually a web browser control. Wouldn't this just delete the com object which I wish to use in the query interface statement?

##### Share on other sites
guddi1    122
It works now. I am not getting that error anymore. I paid a little more attention to why the value of the com object was null when I expected it to be something else. I was using the com object before I had initialized it. So, now I initialize it and then use it with query interface and it works.

Thanks for pointing out my mistake.