roslyn
roslyn copied to clipboard
Fix NRE in rename
NRE might happen when typing using rename.
I have experienced this most in cases when I open a solution first, use rename. Then open a second solution, use rename again and VS would crash in this case.
Look into the call stack:
It shows that the
identifier
typed in UI propagates to InlineRenameUndoManager
but currentState
is null at that moment.
The sequence is:
ActiveSession change
-> InlineRenameService_ActiveSessionChanged
-> Register & Unregister InlineRenameSession_ReplacementTextChanged event handler
(Where the NRE happens)
Moreover, the active InlineRenameSevice
is referencing a different UndoManager (comparing to this
)
So the problem is:
UndoManager
is registered in a workspace factory
Later InlineRenameService_ActiveSessionChanged
hooks to ActiveSession changed event in ctor and it never gets unplug.
So in this case, when another solution is open, sometimes another UndoManager
is created and two managers are hooked to the ActiveSession changed event. But one of them is clean after Disconnect is called so NRE is thrown.
Solution:
Register InlineRenameService_ActiveSessionChanged
in CreateInitialState()
method. CreateInitialState()
is only called when RenameSession get created
Then unregister when UndoManager gets disconnected. It will be called when DismissUIAndRollbackEditsAndEndRenameSessionAsync is called (only happen when commit & cancel)