Sign in to follow this  
guddi1

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this