-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ribbon Sample: Excel crashes after closing #8
Comments
Removed SetSourceData call as it seems like the chart picks the right source when it's created
Fixed via #9 |
I could not recreate this problem (including on 64-bit Excel). |
Do you wait after you close Excel? Does the Excel process unload? |
Yes. It takes a few seconds and then it closes. |
Do you build it with VS 2015? Maybe build environment is different in some way... Can you try with the attached built xll? I just can't comprehend... It reproduces on my dev PC and two VMs each and every time :). All of them have Windows 10 and Office 2016 x64 (two Office 365 and one ProPlus). |
Never crashes. |
Other add-ins running? |
I had some of them enabled, but tried to disable all of them and still have a crash. |
Ok, I finally got one PC when the crash doesn't seem to appear. Faulting application name: EXCEL.EXE, version: 16.0.9226.2156, time stamp: 0x5af64083 The same error appears in the logs on other machines where I actually get the message about the crash. |
Yes - I see the same in the event log Not sure what to think about it. I'd suggest trying to recreate the crash in VBA. |
Actually looks like a null reference. This is with a debugger attached:
|
It seems like this is definitely somehow related to COM objects releasing... If I modify the
... it doesn't crash :). |
Ugh. I suppose you could try that in the ribbon OnDisconnect callback. I tried to fiddle in VBA a bit, but can't run into problems there. Maybe with a VSTO add-in... |
Yeah, that code placed into Yeah, last time my colleague tried to report something to MS it took about a year to narrow down the problem and another year to realize that the fix will break other things and therefore it's better to leave it as is. And yes, that was related to COM too :). |
As I had a similar problem with Excel crashing after being closed, I wanted to share some insights I gathered around this and my solution. Public Class MenuHandler
Inherits CustomUI.ExcelRibbon
Public Overrides Sub OnDisconnection(RemoveMode As ExcelDna.Integration.Extensibility.ext_DisconnectMode, ByRef custom As Array)
For Each aKey As String In Functions.StatusCollection.Keys
Dim clearRange As Excel.Range = Functions.StatusCollection(aKey).formulaRange
If Not IsNothing(clearRange) Then Marshal.ReleaseComObject(clearRange)
Next
For Each DBmodifType As String In DBModifDefColl.Keys
For Each dbmapdefkey As String In DBModifDefColl(DBmodifType).Keys
Dim clearRange As Excel.Range = DBModifDefColl(DBmodifType).Item(dbmapdefkey).getTargetRange()
If Not IsNothing(clearRange) Then Marshal.ReleaseComObject(clearRange)
Next
Next
Do
GC.Collect()
GC.WaitForPendingFinalizers()
Loop While (Marshal.AreComObjectsAvailableForCleanup())
End Sub
... I have left the Thank you Govert. |
Another reason that I have uncovered now: Any shared or static class variables should be released on finalizing the class, I have some of these in the AddInEvents class used for handling events: <ComVisible(True)>
Public Class AddInEvents
Implements IExcelAddIn
'CommandButton that can be inserted on a worksheet
Shared WithEvents cb1 As Forms.CommandButton
'CommandButton that can be inserted on a worksheet
Shared WithEvents cb2 As Forms.CommandButton
'CommandButton that can be inserted on a worksheet
Shared WithEvents cb3 As Forms.CommandButton
...
Protected Overrides Sub Finalize()
MyBase.Finalize()
If Not IsNothing(cb1) Then Marshal.ReleaseComObject(cb1)
If Not IsNothing(cb2) Then Marshal.ReleaseComObject(cb2)
If Not IsNothing(cb3) Then Marshal.ReleaseComObject(cb3)
...
End Sub
End Class I know this is a rather advanced topic, but sooner or later someone will run into those, so maybe this should be documented somewhere... |
Tried on Excel 2016 x64 Version 1803 (Build 9126.2152 Click-to-Run).
Tried to update ExcelDna NuGet packages to v0.34.6.
If I don't click My Button and just close Excel right away - it doesn't crash.
The text was updated successfully, but these errors were encountered: