Excel 2007 crashes after unloading Add-In

Hi there,

I've seem to run into an interesting problem with an Add-In that I originally wrote for Excel 2003 and below, and then tried to run with Excel 2007. The problem is that even though my Add-In works perfectly while Excel is running, when I close Excel, shortly after the Addin's are unloaded and the GUI goes away I get what appears to be a Null Reference exception.

The Add-In is written in ATL C++ and uses the IDispatchImpl and IDispEventSimpleImpl interfaces to communicate events with Excel.

I've tried to debug the code to find out what is happening, but there are no exceptions thrown anywhere in my code. I can go all the way to the point where Excel calls my OnDisconnection() and subsequently OnBeginShutdown() functions at which point I return S_OK. It is then, if I walk through some of the assembly code in Excel I get to a point where a NULL reference exception is encountered.

The only thing my Add-In does is create a new Command Bar and adds a button to it. By the process of commenting out all of the code inside my functions and then slowly enabling some I've found that the code that is somehow causing this problem is the code that creates my Command Bar and/or gets its position before the Add-In closes.

The code I use to create the command bar is fired in the OnStartupComplete() event of the IDispatch interface and does this:

//The collection of Command Bars used to add my new one
CComQIPtr<Office::_CommandBars> oCommandBars;

//Setup the application
//Note: m_pApplication is an LDispatch* that is given by Excel
//at StartupComplete()
CApplication spApp(m_pApplication);

//Get the collection of command bars
oCommandBars = spApp.get_CommandBars();

//Now add a new toolband to which we'll add 2 buttons
CComVariant vName("MyBar");
CComVariant vEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
CComVariant vVisible(VARIANT_TRUE);
CComQIPtr<Office::CommandBar> oMyBar;

//Loop through the collection of command bars and see if our bar
//already exists

BSTR tmp;
int count;
oCommandBars->get_Count(&count);
for(int i = 1; i < count; i++)
{
CComVariant vIndex(i);
oCommandBars->get_Item(vIndex, &oMyBar);
oMyBar->get_Name(&tmp);
if(strcmp(ConvertBSTRToString(tmp), "MyBar"))
{
oMyBar.Detach();
}
else
break;
}
//free the string
::SysFreeString(tmp);

//If we didn't find the bar already there, create it
if(!oMyBar)
{

//position it below all toolbands
//MsoBarPosition::msoBarTop = 1
CComVariant vPos(1);
CComVariant vTemp(VARIANT_TRUE); //menu is temporary

//Add a new toolband through Add method
//vMenuTemp holds an unspecified parameter
//oMyBar points to the newly created toolband
oCommandBars->Add(vName, vPos, vEmpty, vTemp, &oMyBar);
}

Instead of using the regular ATL convention of ComQIPtr<Excel::_Application>, I used a MFC class creator for COM interfaces to create the wrapper class (CApplication) to make the code simpler. This has worked fine for me before, and I would change it but I'd have to wade through a bunch of conflicts while including the Excel type library directly.

Any help or direction on this would be greatly appreciated! Also, please let me know what additional information I can give, as I'm open for trying almost anything.
[3647 byte] By [m4stermel] at [2007-11-20 11:30:38]
# 1 Re: Excel 2007 crashes after unloading Add-In
CComQIPtr<Office::CommandBar> oMyBar;
Dunno for sure, but I guess this is a pointer. I don't see that you assign it to something.

oMyBar.Detach();
If it is a pointer then this doesn't compile.
Skizmo at 2007-11-10 22:25:12 >
# 2 Re: Excel 2007 crashes after unloading Add-In
This is a pointer of sorts, rather a wrapped pointer.
CComQIPtr<Office::CommandBar> oMyBar;

Before I call Detach() on it, it is assigned inside the for loop here:
oCommandBars->get_Item(vIndex, &oMyBar);
Then if it's name doesn't match that of my bar, I tell the wrapped COM pointer to dispose of this handle.

I also assign it here:

//Add a new toolband through Add method
//vEmpty holds an unspecified parameter
//oMyBar points to the newly created toolband
oCommandBars->Add(vName, vPos, vEmpty, vTemp, &oMyBar);

This actually tells Excel to create a new toolbar inside its collection of toolbars and hand me back a handle to it.
--------------------------------

I've also found out a couple more interesting "flaky" behaviors about this bug. When I run the program from inside Visual Studio 2003 (which is the IDE I'm using), and I put a breakpoint anywhere in my active code (or even anywhere in the assembly of Excel) this exception never happens. But if I just start the debugging without any breakpoints, or run Excel manually outsitde VS, I will get this bug.

Another thing is when I restart the computer (any computer running the Add-In, not just mine) the first time I run Excel this exception won't occur when it closes. But every time after that it will always crash on this exception when it closes. Is that weird or what?!?!

I've also done some research online and found a rather dated incident where a common "Send to Bluetooth" Excel Add-In caused the exact same behavior with Excel 2007. I think the add-in might have been developed by Broadcomm, but I'm not sure. So needless to say I can't find out what they did to fix it.

If there's anyone out there that's seen any of this behavior before, related or not, I'd really love to hear about it. I could use all the help I can get... short of scratching the whole thing and writing a completely seperate Add-In just for Excel 2007.

Thanks.
m4stermel at 2007-11-10 22:26:23 >