Calling unadvise inside the event itself could lead to random crashs?

S

s3ven

Hello,

I'm programming with C++/ATL and i'm using the Vernoter sample. Testing
on Ol2003 with typelibe of OL2003

I'm experiencing random crashs of outlook when its closed : the crash
don't happen always it can takes some outlook restart to occur and when
i remove the call to DispEventUnadvise(m_pExplorer) in OnExplorerClose
(but leave the .Release and =NULL parts) the plugin stop crashing...

But sometimes (1/20) outlook remains in memory (i guess it's because no
Unadvise is called for the Explorer Events).

I want to add my Command Bar only to the main Explorer. How and where
should i free memory allocated to the Explorer's events?
(OnSelectionChange,Onbeforefolderswitch,OnExplorerClose)

Below are some parts of the code, thanks in advance for any
suggestions! I'm really stuck on this..



void _stdcall CApplication::OnExplorerClose(){

if (gPreviousFolder!=NULL)
{gPreviousFolder.Release();gPreviousFolder=NULL;}

if (idtimer!=NULL) {KillTimer(0,idtimer);}
if (idtimer_exp!=NULL) { KillTimer(0,idtimer_exp); }

if (m_pCallButton != NULL)
CommandButtonEvents::DispEventUnadvise(m_pCallButton);

if (m_pCallButton!=NULL)
{m_pCallButton.Release();m_pCallButton=NULL;}

if (m_pExplorer.p != NULL)
{
HRESULT hr=OutlookExpEvents::DispEventUnadvise(m_pExplorer);//is it ok
to call it here?
m_pExplorer.Release();m_pExplorer=NULL;
}

}


STDMETHODIMP CApplication::OnConnection(IDispatch *HostApp,
ext_ConnectMode ConnectMode, IDispatch *AddinInst, VARIANT *custom)
{
TCHAR szHomePath[_MAX_PATH] = {NULL};
TCHAR szDefaultSubject[MAX_SUBJECT_LENGHT] = {NULL};
DWORD dwCount =_MAX_PATH;
CRegKey RegKey;
CComBSTR bsKeyName, bsProjName;
long result = ~ERROR_SUCCESS;
vide.scode = DISP_E_PARAMNOTFOUND; vide.vt=VT_ERROR;


gpCApp = this;

idtimer=NULL; idtimer_exp=NULL;

m_pHostApp.Attach((Outlook::_Application *)HostApp);
gHostApp.Attach((Outlook::_Application *)HostApp);

m_pHostApp->GetNamespace(CComBSTR("MAPI"),&m_pNameSpace);
OutlookAppEvents::DispEventAdvise(m_pHostApp);

if (ConnectMode == ext_cm_AfterStartup)
{
OnStartupComplete(custom);
}

return S_OK;

}


STDMETHODIMP CApplication::OnDisconnection(ext_DisconnectMode
DisconnectMode, VARIANT *custom)
{

if (m_pNameSpace!=NULL)
{m_pNameSpace.Release();m_pNameSpace=NULL;}

if (gHostApp!=NULL) {gHostApp.Release();gHostApp=NULL;}

if (m_pHostApp.p != NULL)
{
HRESULT hr=OutlookAppEvents::DispEventUnadvise(m_pHostApp);
m_pHostApp.Release();
m_pHostApp=NULL;
}

}


STDMETHODIMP CApplication::OnStartupComplete(VARIANT *custom)
{

HRESULT hr = m_pHostApp->ActiveExplorer(&m_pExplorer);

if (SUCCEEDED(hr) && (m_pExplorer!=NULL)) {
OutlookExpEvents::DispEventAdvise(m_pExplorer);
} else
{
// if started with no explorer (minimized,mailto://,actives)
}

return S_OK;
}
 
D

Dmitry Streblechenko

Hmmm... I never had any problems with doing the cleanup in the
Explorer.Close event (including unadvising the explorer events are releasing
the explorer).

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

s3ven said:
Hello,

I'm programming with C++/ATL and i'm using the Vernoter sample. Testing
on Ol2003 with typelibe of OL2003

I'm experiencing random crashs of outlook when its closed : the crash
don't happen always it can takes some outlook restart to occur and when
i remove the call to DispEventUnadvise(m_pExplorer) in OnExplorerClose
(but leave the .Release and =NULL parts) the plugin stop crashing...

But sometimes (1/20) outlook remains in memory (i guess it's because no
Unadvise is called for the Explorer Events).

I want to add my Command Bar only to the main Explorer. How and where
should i free memory allocated to the Explorer's events?
(OnSelectionChange,Onbeforefolderswitch,OnExplorerClose)

Below are some parts of the code, thanks in advance for any
suggestions! I'm really stuck on this..



void _stdcall CApplication::OnExplorerClose(){

if (gPreviousFolder!=NULL)
{gPreviousFolder.Release();gPreviousFolder=NULL;}

if (idtimer!=NULL) {KillTimer(0,idtimer);}
if (idtimer_exp!=NULL) { KillTimer(0,idtimer_exp); }

if (m_pCallButton != NULL)
CommandButtonEvents::DispEventUnadvise(m_pCallButton);

if (m_pCallButton!=NULL)
{m_pCallButton.Release();m_pCallButton=NULL;}

if (m_pExplorer.p != NULL)
{
HRESULT hr=OutlookExpEvents::DispEventUnadvise(m_pExplorer);//is it ok
to call it here?
m_pExplorer.Release();m_pExplorer=NULL;
}

}


STDMETHODIMP CApplication::OnConnection(IDispatch *HostApp,
ext_ConnectMode ConnectMode, IDispatch *AddinInst, VARIANT *custom)
{
TCHAR szHomePath[_MAX_PATH] = {NULL};
TCHAR szDefaultSubject[MAX_SUBJECT_LENGHT] = {NULL};
DWORD dwCount =_MAX_PATH;
CRegKey RegKey;
CComBSTR bsKeyName, bsProjName;
long result = ~ERROR_SUCCESS;
vide.scode = DISP_E_PARAMNOTFOUND; vide.vt=VT_ERROR;


gpCApp = this;

idtimer=NULL; idtimer_exp=NULL;

m_pHostApp.Attach((Outlook::_Application *)HostApp);
gHostApp.Attach((Outlook::_Application *)HostApp);

m_pHostApp->GetNamespace(CComBSTR("MAPI"),&m_pNameSpace);
OutlookAppEvents::DispEventAdvise(m_pHostApp);

if (ConnectMode == ext_cm_AfterStartup)
{
OnStartupComplete(custom);
}

return S_OK;

}


STDMETHODIMP CApplication::OnDisconnection(ext_DisconnectMode
DisconnectMode, VARIANT *custom)
{

if (m_pNameSpace!=NULL)
{m_pNameSpace.Release();m_pNameSpace=NULL;}

if (gHostApp!=NULL) {gHostApp.Release();gHostApp=NULL;}

if (m_pHostApp.p != NULL)
{
HRESULT hr=OutlookAppEvents::DispEventUnadvise(m_pHostApp);
m_pHostApp.Release();
m_pHostApp=NULL;
}

}


STDMETHODIMP CApplication::OnStartupComplete(VARIANT *custom)
{

HRESULT hr = m_pHostApp->ActiveExplorer(&m_pExplorer);

if (SUCCEEDED(hr) && (m_pExplorer!=NULL)) {
OutlookExpEvents::DispEventAdvise(m_pExplorer);
} else
{
// if started with no explorer (minimized,mailto://,actives)
}

return S_OK;
}
 
H

Henry Gusakovsky

Hi

why do you Attach to the Application object.
Attach does not AddRef pointer.

But in OnDisconnect you release this pointers.

You can enable Paging Heap so you will see when some code uses deallocated
memory. To do this use gflags utility from Windows Debugging Tools.

WBR
Henry

s3ven said:
Hello,

I'm programming with C++/ATL and i'm using the Vernoter sample. Testing
on Ol2003 with typelibe of OL2003

I'm experiencing random crashs of outlook when its closed : the crash
don't happen always it can takes some outlook restart to occur and when
i remove the call to DispEventUnadvise(m_pExplorer) in OnExplorerClose
(but leave the .Release and =NULL parts) the plugin stop crashing...

But sometimes (1/20) outlook remains in memory (i guess it's because no
Unadvise is called for the Explorer Events).

I want to add my Command Bar only to the main Explorer. How and where
should i free memory allocated to the Explorer's events?
(OnSelectionChange,Onbeforefolderswitch,OnExplorerClose)

Below are some parts of the code, thanks in advance for any
suggestions! I'm really stuck on this..



void _stdcall CApplication::OnExplorerClose(){

if (gPreviousFolder!=NULL)
{gPreviousFolder.Release();gPreviousFolder=NULL;}

if (idtimer!=NULL) {KillTimer(0,idtimer);}
if (idtimer_exp!=NULL) { KillTimer(0,idtimer_exp); }

if (m_pCallButton != NULL)
CommandButtonEvents::DispEventUnadvise(m_pCallButton);

if (m_pCallButton!=NULL)
{m_pCallButton.Release();m_pCallButton=NULL;}

if (m_pExplorer.p != NULL)
{
HRESULT hr=OutlookExpEvents::DispEventUnadvise(m_pExplorer);//is it ok
to call it here?
m_pExplorer.Release();m_pExplorer=NULL;
}

}


STDMETHODIMP CApplication::OnConnection(IDispatch *HostApp,
ext_ConnectMode ConnectMode, IDispatch *AddinInst, VARIANT *custom)
{
TCHAR szHomePath[_MAX_PATH] = {NULL};
TCHAR szDefaultSubject[MAX_SUBJECT_LENGHT] = {NULL};
DWORD dwCount =_MAX_PATH;
CRegKey RegKey;
CComBSTR bsKeyName, bsProjName;
long result = ~ERROR_SUCCESS;
vide.scode = DISP_E_PARAMNOTFOUND; vide.vt=VT_ERROR;


gpCApp = this;

idtimer=NULL; idtimer_exp=NULL;

m_pHostApp.Attach((Outlook::_Application *)HostApp);
gHostApp.Attach((Outlook::_Application *)HostApp);

m_pHostApp->GetNamespace(CComBSTR("MAPI"),&m_pNameSpace);
OutlookAppEvents::DispEventAdvise(m_pHostApp);

if (ConnectMode == ext_cm_AfterStartup)
{
OnStartupComplete(custom);
}

return S_OK;

}


STDMETHODIMP CApplication::OnDisconnection(ext_DisconnectMode
DisconnectMode, VARIANT *custom)
{

if (m_pNameSpace!=NULL)
{m_pNameSpace.Release();m_pNameSpace=NULL;}

if (gHostApp!=NULL) {gHostApp.Release();gHostApp=NULL;}

if (m_pHostApp.p != NULL)
{
HRESULT hr=OutlookAppEvents::DispEventUnadvise(m_pHostApp);
m_pHostApp.Release();
m_pHostApp=NULL;
}

}


STDMETHODIMP CApplication::OnStartupComplete(VARIANT *custom)
{

HRESULT hr = m_pHostApp->ActiveExplorer(&m_pExplorer);

if (SUCCEEDED(hr) && (m_pExplorer!=NULL)) {
OutlookExpEvents::DispEventAdvise(m_pExplorer);
} else
{
// if started with no explorer (minimized,mailto://,actives)
}

return S_OK;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top