Switching for COInitialize to OleInitialize crashes app

From: Peter Carlson (peter_at___NO__SPAM___howudodat.com)
Date: 05/28/04


Date: Fri, 28 May 2004 16:08:10 -0600

A simple WTL SDI app with the following helper class crashes after a switch
from CoInitialize and CoUnitialize to OleInitialize and OleUninit....()

If EventSinking is turned on (DispEventAdvise) then the app crashes with a
access violation in the Unadvise routine. If event sinking is not turned on
then the app crashes in ~CComPtrBase, p->release. Call stacks are show for
each crash. If I switch back to CoInit... then it doesn't crash. Any
ideas? - Peter

The browser control is created with a call to:
  browser1.Create(m_hWnd, rcDefault, NULL, WS_VISIBLE| WS_CHILD,
WS_EX_STATICEDGE);

Call Stack for DispEventUnadvise Crash
  htmltest.exe!ATL::AtlUnadvise(IUnknown * pUnkCP=0x0015efe8, const _GUID &
iid={...}, unsigned long dw=1)

htmltest.exe!ATL::IDispEventSimpleImpl<1001,CWndBrowser,&GUID_NULL>::DispEve
ntUnadvise(IUnknown * pUnk=0x0015efe8, const _GUID * piid=0x0012fbfc)
  htmltest.exe!CWndBrowser::~CWndBrowser()
  htmltest.exe!CHtmltestView::~CHtmltestView()
  htmltest.exe!CMainFrame::~CMainFrame()
  htmltest.exe!Run(char * __formal=0x00151f14, int nCmdShow=1)
<snipped>

Call Stack for ~CComPtrBase Crash
  htmltest.exe!ATL::CComPtrBase<IWebBrowser2>::~CComPtrBase<IWebBrowser2>()
  htmltest.exe!ATL::CComPtr<IWebBrowser2>::~CComPtr<IWebBrowser2>()

htmltest.exe!ATL::CComQIPtr<IWebBrowser2,&_GUID_d30c1661_cdaf_11d0_8a3e_00c0
4fc9e26e>::~CComQIPtr<IWebBrowser2,&_GUID_d30c1661_cdaf_11d0_8a3e_00c04fc9e2
6e>()
  htmltest.exe!CWndBrowser::~CWndBrowser()
  htmltest.exe!CHtmltestView::~CHtmltestView()
  htmltest.exe!CMainFrame::~CMainFrame()
  htmltest.exe!Run(char * __formal=0x00151f14, int nCmdShow=1)
<snipped>

Here is the CWndBrowser Class
class CWndBrowser :
 public CWindowImpl<CWndBrowser, CAxWindow>,
 public IDispEventImpl<1001, CWndBrowser>,
 public CMessageFilter
{
public:
 CWndBrowser() {}
 ~CWndBrowser(void) {
// DispEventUnadvise(pWB,&m_iid);
 CMessageLoop* pLoop = _Module.GetMessageLoop();
 if (pLoop) pLoop->RemoveMessageFilter(this);
};

 //navigate commands
 void Navigate(TCHAR *url){
 CComVariant v;
 if ( pWB ) pWB->Navigate ( CComBSTR(url), &v, &v, &v, &v );
}

 BOOL PreTranslateMessage(MSG* pMsg) {
 // process only child messagse
 if((pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST) &&
 (pMsg->message < WM_MOUSEFIRST || pMsg->message > WM_MOUSELAST))
  return FALSE;

  // give HTML page a chance to translate this message
  return (BOOL)SendMessage(WM_FORWARDMSG, 0, (LPARAM)pMsg);
}

 BEGIN_MSG_MAP(CWndBrowser)
  MESSAGE_HANDLER(WM_CREATE, OnCreate)
  DEFAULT_REFLECTION_HANDLER()
 END_MSG_MAP()
 LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL&
/*bHandled*/){
 CComPtr<IUnknown> punkCtrl;
 CComVariant v;

 CreateControlEx ( L"Shell.Explorer", NULL, NULL, &punkCtrl );
 pWB = punkCtrl;
 if ( pWB ) pWB->Navigate ( CComBSTR("about:blank"), &v, &v, &v, &v );

 // Create and wire up event sink to control.
// HRESULT hr = AtlGetObjectSourceInterface(pWB, &m_libid, &m_iid,
&m_wMajorVerNum, &m_wMinorVerNum);
// if (SUCCEEDED(hr)) hr = DispEventAdvise(pWB, &m_iid);

 CMessageLoop* pLoop = _Module.GetMessageLoop();
 ATLASSERT(pLoop != NULL);
 pLoop->AddMessageFilter(this);

 return 0;
}

BEGIN_SINK_MAP(CWndBrowser)
 SINK_ENTRY(1001, 0x00000103, OnDocumentComplete)
 SINK_ENTRY(1001, 0x000000FA, OnBeforeNavigate2Explorer1)
 SINK_ENTRY(1001, 0x000000FC, OnNavigateComplete2Explorer1)
END_SINK_MAP()

 HRESULT __stdcall OnDocumentComplete(LPDISPATCH pDisp, VARIANT FAR* URL)
{ return S_OK; }
 HRESULT __stdcall OnBeforeNavigate2Explorer1(LPDISPATCH pDisp, VARIANT FAR*
URL, VARIANT FAR* Flags, VARIANT FAR* TargetFrameName, VARIANT FAR*
PostData, VARIANT FAR* Headers, BOOL FAR* Cancel) { return S_OK; }
 HRESULT __stdcall OnNavigateComplete2Explorer1(LPDISPATCH pDisp, VARIANT
FAR* URL) { return S_OK; }

protected:
 CComQIPtr<IWebBrowser2> pWB;
};



Relevant Pages

  • Re: Problems with GetOpenFileName and GetSaveFileName
    ... > sometimes crash if there were problems with the app ... You see thats the problem - it only crashes in the Desktop Folder no where ... Don't exit the app, ... I'll give it a go if your program also crashes. ...
    (microsoft.public.win32.programmer.ui)
  • Re: TAdoconnection component causes crash in my CGI webapp
    ... > Perhaps you don't call CoInitialize. ... That's can be a reason why the app ... > crashes. ...
    (borland.public.delphi.database.ado)
  • SHBrowseForFolder and BIF_USENEWUI
    ... I'm having an issue using SHBrowseForFolder with the BIF_USENEWUI flag. ... the pidlRoot member is set to anything other than CSIDL_DESKTOP, the app ... crashes with an application error: The memory could not be read. ... Using CoInitialize() didn't do any good either. ...
    (microsoft.public.vb.winapi)
  • inetinfo keeps crashing on backend server
    ... This application crashes about every minute and a half. ... the last app to start before the crash is OMA Categorizer, ...
    (microsoft.public.exchange.admin)
  • Several, bring coffie
    ... >>> The point is that not that RosAsm crashes. ... Obviously the best way to prevent this> happening is to make an application which never crashes, but,> even then, there are events which can cause a crash -- such as ... this is not a LuxAsm problem...this ...
    (alt.lang.asm)