From 238c0a239110803fb5956b47d3fc7461db46ebd9 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 27 Nov 2010 00:06:03 +0100 Subject: [PATCH] Fixed NullReferenceException in MSBuildBasedProject.get_MinimumSolutionVersion() when removing a project from the solution while the build is running. (http://community.sharpdevelop.net/forums/t/12316.aspx) --- src/Main/Base/Project/Src/Project/BuildEngine.cs | 14 +++++++++++++- .../Project/Src/Project/MSBuildBasedProject.cs | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Main/Base/Project/Src/Project/BuildEngine.cs b/src/Main/Base/Project/Src/Project/BuildEngine.cs index 567185b827..ce22c1fb9d 100644 --- a/src/Main/Base/Project/Src/Project/BuildEngine.cs +++ b/src/Main/Base/Project/Src/Project/BuildEngine.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Text; using System.Threading; @@ -274,7 +275,15 @@ namespace ICSharpCode.SharpDevelop.Project public void DoStartBuild(object state) { - project.StartBuild(options, this); + string name = string.Empty; + try { + name = project.Name; + project.StartBuild(options, this); + } catch (ObjectDisposedException) { + // Handle ObjectDisposedException that occurs when trying to build a project that was unloaded. + ReportError(new BuildError(null, "The project '" + name + "' was unloaded.")); + Done(false); + } } public void ReportError(BuildError error) @@ -630,6 +639,9 @@ namespace ICSharpCode.SharpDevelop.Project } } if (messagesToReport != null) { + // we can report these messages outside the lock: + // while they swap order with messages currently coming in (ReportMessage), + // this shouldn't be a problem as nodes should not report messages after they finish building. messagesToReport.ForEach(ReportMessageInternal); } if (newNodeWithOutputLockAlreadyFinishedBuilding) { diff --git a/src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs b/src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs index 71be7d3c94..d5282385f5 100644 --- a/src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs +++ b/src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs @@ -98,6 +98,11 @@ namespace ICSharpCode.SharpDevelop.Project public override int MinimumSolutionVersion { get { lock (SyncRoot) { + // This property is called by CSharpProject.StartBuild (and other derived StartBuild methods), + // so it's important that we throw an ObjectDisposedException for disposed projects. + // The build engine will handle this exception (occurs when unloading a project while a build is running) + if (projectFile == null) + throw new ObjectDisposedException("MSBuildBasedProject"); if (string.IsNullOrEmpty(projectFile.ToolsVersion) || projectFile.ToolsVersion == "2.0") { return Solution.SolutionVersionVS2005; } else if (projectFile.ToolsVersion == "3.0" || projectFile.ToolsVersion == "3.5") {