rubberduck-vba / Rubberduck

Every programmer needs a rubberduck. COM add-in for the VBA & VB6 IDE (VBE).
https://rubberduckvba.com
GNU General Public License v3.0
1.92k stars 302 forks source link

Rubberduck Crashes When Enabled from Add-Ins #2163

Closed yeshua-watson-rheem closed 7 years ago

yeshua-watson-rheem commented 8 years ago

I've been pulling my hair out trying to get Rubberduck to work to no avail. Version 2.0.7 installs just fine, but when I open the Code Explorer, it not only doesn't parse the projects, but the refresh button is disabled too. So are all the menu options under the "Add" button. When I attempt to reload the Add-In from Excel's add in menu, I get an error message with a stack trace, and clicking "Continue" crashes Excel entirely. Excel is also crashing when I close it out while Rubberduck is enabled and it suddenly goes through it's recovery loop.

RubberduckLog.txt RubberduckTrace.txt rubberduck.config.txt

There's no traditional forum for this, so the issue tracker was the only thing left. The strange thing is the trace doesn't mention Rubberduck at all. I've updated my system to the point there aren't any updates left and it still doesn't work.

Hosch250 commented 8 years ago

This sounds like a bug. Thanks for the report.

Vogel612 commented 8 years ago

The link "RubberduckTrace.txt" is a 404 for me. As is "rubberduck.config.txt". Could you please check?

Hosch250 commented 8 years ago

First, there is a bug with the key and mouse hooks that does not unload cleanly and causes an exception when RD is reloaded. Second, what does the VBE do when you open it at first? Does the Rubberduck toolbar do anything, or does it stay stuck at Pending?

yeshua-watson-rheem commented 8 years ago

RubberduckTrace.txt rubberduck.config.txt

yeshua-watson-rheem commented 8 years ago

I added the files in a separate comment to avoid them 404-ing again. To answer your question Hosch, it does nothing. Clicking the "Parse" button doesn't to seem to have any effect, and the button is disabled in the Code Explorer. There's also an empty button next to the Parse button in the toolbar that doesn't seem to do anything either. I believe I saw this issue reported in another issue though. See attached.

vbe_editor

Hosch250 commented 8 years ago

Looks like something is broken alright.

yeshua-watson-rheem commented 8 years ago

Just an update, the today's release didn't solve this one either. I was clinching on for hope but it looks like I'm out to sea alone on this one. Any other way to debug? I tried compiling from source but VS13 Express kept throwing out error messages about the XML attribute not being in the spec and refusing to compile.

Vogel612 commented 8 years ago

That shouldn't happen. Do you have a full error message on that compilation failure? With some luck I can take a stab at that tomorrow ...

Hosch250 commented 8 years ago

I've never tested RD with a .xlsb file. I'll take a look later.

yeshua-watson-rheem commented 8 years ago

@Vogel612 I'll have to reinstall VS13 again. I was getting desperate and tried to eliminate all possible interference with RD that I could think of short of a full blown OS reinstall.

From my google history, looks like the error was: "The tag 'Interaction.Behaviors' does not exist in XML namespace 'http://schemas.microsoft.com/expression/2010/interactivity'"

Vogel612 commented 8 years ago

@yeshua-watson-rheem please check the references of Rubberduck when you open the Project in Visual Studio. I don't need it on VS 2015 express, but this stackoverflow q&a mentions you need a reference to Microsoft.Expression.Interactions AND System.Windows.Interactivity

From what I can tell, the project doesn't have Microsoft.Expression.Interactions. Could you try adding that and tell me if it still errors?

yeshua-watson-rheem commented 8 years ago

@Vogel612 I can't try it in 2013 again as 2013 isn't available anymore for my employer as we've moved on to 2015. 2015 compiled just fine though, so there's that useless validation. :-)

yeshua-watson-rheem commented 8 years ago

Is there anyway to run RD's parser from the immediate window?

retailcoder commented 8 years ago

@yeshua-watson-rheem the add-in, no. But you can reference the Rubberduck TLB and play with the COM API (still experimental) - you can create a ParserState object and request a parse of everything in the VBE, and then iterate and filter declarations and identifier references. But that COM object won't be talking to the managed (.net) objects Rubberduck uses so no, you can't interact with the add-in from the immediate pane.

ThunderFrame commented 8 years ago

Further to comments in chat:

How to build an Office COM add-in by using Visual C# .NET

Both the OnStartupComplete method and the OnBeginShutdown method are called when the host application has left or is entering a state in which user interaction should be avoided because the application is busy loading or unloading itself from memory. The OnStartupComplete method is called only if the add-in was connected during startup, and the OnBeginShutdown method is called only if the host disconnects the add-in during shutdown.

Because the user interface for the host application is fully active when these events fire, they may be the only way to perform certain actions that otherwise would be unavailable from the OnConnection event and the OnDisconnection event.

HOWTO: Use correctly the OnConnection method of a Visual Studio add-in seems to apply to VBA/VB6 too.

However, when the add-in is loaded on startup and the OnConnection method is called by Visual Studio with the ext_ConnectMode.ext_cm_Startup connect mode, Visual Studio is not fully initialized. While many add-ins can be initialized correctly at this point, some things will fail. For example, if your add-in shows a toolwindow when loaded, the toolwindow won't be shown when the add-in is loaded on startup, but it is shown when the add-in is loaded by hand with the Add-In Manager.

IDTExtensibility2.OnStartupComplete Method

On occasion, OnConnection does not occur correctly, such as when an add-in is loaded, but a component required by an add-in has not yet loaded. This is unusually due to the fact that Visual Studio has not yet started completely. Using OnStartupComplete guarantees that the Visual Studio integrated development environment (IDE) has completed the startup process.

IDTExtensibility2.OnBeginShutdown Method

Although a shutdown of Visual Studio might be canceled, the OnBeginShutdown method cannot be canceled. As a result, add-ins should assume that all shutdown events occur and perform any cleanup routines accordingly.

This method can be used to store the settings of the add-in at the time that Visual Studio shuts down so that it can be restored later to its original state.

So, I think that means the IDTExtensibility implementation looks something like this (somewhat bastardized MZTools-pseudo VBA code).

Public Sub OnConnection(ByVal application As Object, ByVal connectMode _ 
   As Extensibility.ext_ConnectMode, ByVal addInInst As Object, _
   ByRef custom As System.Array)

   applicationObject = application
   addInInstance = AddIn

   Select Case connectMode
      Case ext_ConnectMode.ext_cm_Startup
         ' The add-in was marked to load on startup 
         ' Do nothing at this point because the IDE may not be fully initialized 

      Case ext_ConnectMode.ext_cm_AfterStartup 
         'Startup was already completed...
         ' The add-in was loaded from the Addins Dialog.
         InitializeAddin

    End Select

End Sub

Public Sub OnStartupComplete(ByRef custom As System.Array)
   InitializeAddin
End Sub

Public Sub OnBeginShutdown(ByRef custom As System.Array)
  'We have no choice but to assume host will shutdown, and we can't cancel...
  '...So cleanup
  CleanupAddin
End Sub

Public Sub OnDisconnection(RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) 
   Select Case RemoveMode
      Case ext_DisconnectMode.ext_dm_HostShutdown
         ' The add-in is disconnected when the host application closes.
         ' Hopefully, we already cleaned up in the OnBeginShutdown event

      Case ext_DisconnectMode.ext_dm_UserClosed
         'The add-in is disconnected by the end user or an Automation controller
         CleanupAddin

    End Select

End Sub

Private Sub InitializeAddin()
  'Do initialization...  
End Sub

Private Sub CleanupAddin()
  'Do actual cleanup....
End Sub
retailcoder commented 8 years ago

FYI v2.0.9 RC6 is about to be issued; I'm able to correctly load and unload RD from the addins manager dialog, but not to reload, because the teardown code is basically assuming that RD tears down along with its host.

Trying to reload RD after it has released all its resources throws a NullReferenceException when the startup code tries to attach some toolwindow to a no-longer-existing COM resource.