[f-spot] Fix unsafe singleton construction.



commit d617121202f8b1a0ea78770bb77e2d40c568516d
Author: Ruben Vermeersch <ruben savanne be>
Date:   Sat Jun 19 21:58:40 2010 +0200

    Fix unsafe singleton construction.
    
    This commit adds a check that make sure there are no recursive calls to
    App.Instance.Organizer during construction. Should avoid the two
    MainWindow-problem.

 src/Core/App.cs |   44 ++++++++++++++++++++++++++++++--------------
 1 files changed, 30 insertions(+), 14 deletions(-)
---
diff --git a/src/Core/App.cs b/src/Core/App.cs
index ccd1328..93318f4 100644
--- a/src/Core/App.cs
+++ b/src/Core/App.cs
@@ -13,6 +13,7 @@ using System;
 using System.IO;
 using System.Collections.Generic;
 using System.Linq;
+using System.Threading;
 
 using Unique;
 
@@ -24,21 +25,34 @@ namespace FSpot
 {
 	public class App : Unique.App
 	{
+		static object sync_handle = new object ();
+
 #region public API
 		static App app;
 		public static App Instance {
 			get {
-				if (app == null)
-					app = new App ();
+				lock (sync_handle) {
+					if (app == null)
+						app = new App ();
+				}
 				return app;
 			}
 		}
 
+		Thread constructing_organizer = null;
+
 		public MainWindow Organizer {
 			get {
-				if (organizer == null) {
-					organizer = new MainWindow (Database);
-					Register (organizer.Window);
+				lock (sync_handle) {
+					if (organizer == null) {
+						if (constructing_organizer == Thread.CurrentThread) {
+							throw new Exception ("Recursively tried to acquire App.Organizer!");
+						}
+
+						constructing_organizer = Thread.CurrentThread;
+						organizer = new MainWindow (Database);
+						Register (organizer.Window);
+					}
 				}
 				return organizer;
 			}
@@ -46,17 +60,19 @@ namespace FSpot
 
 		public Db Database {
 			get {
-				if (db == null) {
-					if (!File.Exists (FSpot.Global.BaseDirectory))
-						Directory.CreateDirectory (FSpot.Global.BaseDirectory);
-
-					db = new Db ();
-
-					try {
-						db.Init (Path.Combine (FSpot.Global.BaseDirectory, "photos.db"), true);
-					} catch (Exception e) {
-						new FSpot.UI.Dialog.RepairDbDialog (e, db.Repair (), null);
-						db.Init (Path.Combine (FSpot.Global.BaseDirectory, "photos.db"), true);
+				lock (sync_handle) {
+					if (db == null) {
+						if (!File.Exists (FSpot.Global.BaseDirectory))
+							Directory.CreateDirectory (FSpot.Global.BaseDirectory);
+
+						db = new Db ();
+
+						try {
+							db.Init (Path.Combine (FSpot.Global.BaseDirectory, "photos.db"), true);
+						} catch (Exception e) {
+							new FSpot.UI.Dialog.RepairDbDialog (e, db.Repair (), null);
+							db.Init (Path.Combine (FSpot.Global.BaseDirectory, "photos.db"), true);
+						}
 					}
 				}
 				return db;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]