[corlib] Do not cache disposed resource sets.
authorMarek Habersack <grendel@twistedcode.net>
Wed, 4 May 2011 21:30:57 +0000 (23:30 +0200)
committerMarek Habersack <grendel@twistedcode.net>
Wed, 4 May 2011 21:30:57 +0000 (23:30 +0200)
mcs/class/corlib/System.Resources/ResourceManager.cs
mcs/class/corlib/System.Resources/ResourceSet.cs

index 5b8f3d53b8bee6d53b5c79267826eb43d8becbb6..8f2305a57582edc9d0fddd3e57161515a64f2b51 100644 (file)
@@ -44,6 +44,7 @@ namespace System.Resources
        [ComVisible (true)]
        public class ResourceManager
        {
+               static readonly object thisLock = new object ();
                static Hashtable ResourceCache = new Hashtable (); 
                static Hashtable NonExistent = Hashtable.Synchronized (new Hashtable ());
                public static readonly int HeaderVersionNumber = 1;
@@ -194,7 +195,7 @@ namespace System.Resources
                        if (culture == null)
                                culture = CultureInfo.CurrentUICulture;
 
-                       lock (this) {
+                       lock (thisLock) {
                                ResourceSet set = InternalGetResourceSet(culture, true, true);
                                object obj = null;
                                
@@ -229,7 +230,7 @@ namespace System.Resources
                        if (culture == null)
                                throw new ArgumentNullException ("culture");
 
-                       lock (this) {
+                       lock (thisLock) {
                                return InternalGetResourceSet (culture, createIfNotExists, tryParents);
                        }
                }
@@ -247,7 +248,7 @@ namespace System.Resources
                        if (culture == null)
                                culture = CultureInfo.CurrentUICulture;
 
-                       lock (this) {
+                       lock (thisLock) {
                                ResourceSet set = InternalGetResourceSet (culture, true, true);
                                string str = null;
 
@@ -315,7 +316,12 @@ namespace System.Resources
                                throw new ArgumentNullException ("name");
                        if (culture == null)
                                culture = CultureInfo.CurrentUICulture;
-                       ResourceSet set = InternalGetResourceSet (culture, true, true);
+                       ResourceSet set;
+
+                       lock (thisLock) {
+                               set = InternalGetResourceSet (culture, true, true);
+                       }
+                       
                        return set.GetStream (name, ignoreCase);
                }
 
@@ -324,13 +330,24 @@ namespace System.Resources
                        if (culture == null)
                                throw new ArgumentNullException ("key"); // 'key' instead of 'culture' to make a test pass
 
-                       ResourceSet set;
+                       ResourceSet set = null;
                        
                        /* if we already have this resource set, return it */
                        set = (ResourceSet) ResourceSets [culture];
-                       if (set != null)
-                               return set;
+                       if (set != null) {
+                               try {
+                                       if (!set.IsDisposed)
+                                               return set;
+                               } catch {
+                                       // ignore
+                               }
 
+                               ResourceSets.Remove (culture);
+                               if (NonExistent.Contains (culture))
+                                       NonExistent.Remove (culture);
+                               set = null;
+                       }
+                       
                        if (NonExistent.Contains (culture))
                                return null;
 
index d6bb98628443bab2c2ffd943862a215dcc1b284b..f7621e60ba1eec89ad642713fa6cc92af3d45b46 100644 (file)
@@ -52,6 +52,10 @@ namespace System.Resources
                [NonSerialized]
                private bool disposed;
 
+               internal bool IsDisposed {
+                       get { return disposed || Reader == null; }
+               }
+               
                // Constructors
                protected ResourceSet ()
                {
@@ -125,7 +129,7 @@ namespace System.Resources
                [ComVisible (false)]
                public virtual IDictionaryEnumerator GetEnumerator ()
                {
-                       if (disposed)
+                       if (IsDisposed)
                                throw new ObjectDisposedException ("ResourceSet is closed.");
                        ReadResources ();
                        return Table.GetEnumerator();
@@ -140,7 +144,7 @@ namespace System.Resources
                {
                        if (name == null)
                                throw new ArgumentNullException ("name");
-                       if (disposed)
+                       if (IsDisposed)
                                throw new ObjectDisposedException ("ResourceSet is closed.");
 
                        ReadResources ();
@@ -202,7 +206,7 @@ namespace System.Resources
                        if (resources_read)
                                return;
 
-                       if (Reader == null)
+                       if (IsDisposed)
                                throw new ObjectDisposedException ("ResourceSet is closed.");
                        lock (Table) {
                                if (resources_read)
@@ -218,7 +222,7 @@ namespace System.Resources
 
                internal UnmanagedMemoryStream GetStream (string name, bool ignoreCase)
                {
-                       if (Reader == null)
+                       if (IsDisposed)
                                throw new ObjectDisposedException ("ResourceSet is closed.");
 
                        IDictionaryEnumerator i = Reader.GetEnumerator();