Quantcast
Channel: Visual Studio Tools for Office (VSTO) forum
Viewing all articles
Browse latest Browse all 5157

Unexpected corruption of VSTO Document object

$
0
0

Hello,

There is something wrong going on with VSTO document object in the scenario below. First please take a look at sample add-in - very simple one:

	public partial class ThisAddIn
	{
		Dictionary<Word.Document, Document> _docDict = new Dictionary<Word.Document, Document>();


		private void ThisAddIn_Startup(object sender, System.EventArgs e)
		{
			Globals.ThisAddIn.Application.WindowActivate += Application_WindowActivate;
		}

		void Application_WindowActivate(Word.Document Doc, Word.Window Wn)
		{
			Document vstoDoc = null;

			if (_docDict.TryGetValue(Doc, out vstoDoc) && vstoDoc != null)
			{
				// vsto object exists - use it

				try
				{
					Debug.Assert(vstoDoc.FullName == Doc.FullName);
				}
				catch (Exception ex)
				{
					Debug.Write("Should never happen 1!");
				}
			}
			else
			{
				// vsto object not exists - create one

				vstoDoc = Globals.Factory.GetVstoObject(Doc);

				_docDict.Remove(Doc);

				_docDict.Add(Doc, vstoDoc);

				try
				{
					Debug.Assert(vstoDoc.FullName == Doc.FullName);
				}
				catch (Exception ex)
				{
					Debug.Write("Should never happen 2!");
				}
			}
		}


		private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
		{
		}


		#region VSTO generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(ThisAddIn_Startup);
			this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
		}

		#endregion
	}

Having add-in installed in word:

1) open one document form local disk (trusted location - no protected view should happen) (lets call it A);

2) open another document from local disk (trusted location - no protected view should happen) (lets call it B);

3) switch couple of times between two windows to make sure all is fine (so far it is);

4) in one of there windows create new blank document (third word window is opened) (lets call it C);

5) switch couple of times between three opened windows to make sure all is fine (so far it is);

6) close window with blank document (document C) (no question asked, window is closed) - that is fine;

7) at this point when you switch between two remaining word document windows (A and B) "Should never happen 1!" text starts to pop up in debug each time one of these windows gets focus (I'm not sure about a rule here but it usually happens for A).

It seems that things go terribly wrong and when blank document is closed one of the remaining VSTO Document objects gets corrupted. In fact a Shutdown event is raised as a side effect of blank document close and since that moment one of remaining VSTO object becomes unusable (as you can see event vstoObject.FullName fails with some exception). Of course it is not possible to re-create VSTO object because you end up with the same broken instance.

It seems like a bug and a serious one. There is also a chance that I'm doing something wrong.

I'm using Visual Studio 2012 and Office 2010.

Described behavior makes nearly impossible to write robust and stable add-in code.

--

Mark


Viewing all articles
Browse latest Browse all 5157

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>