public void Clean()
        {
            var    c = new WeakList <object>();
            object x = new object();

            using (NoGCRegion.Enter(1000)) {
                AddCollectableItems(c, 3);

                c.InsertFirst(x);
                c.InsertAfter(x, x);
                c.InsertBefore(x, x);

                Assert.AreEqual(6, c.AddCountSinceLastClean);
                Assert.AreEqual(6, c.UnsafeCount);
                Assert.IsTrue(c.Take(3).SequenceEqual(new[] { x, x, x }));
            }

            Helpers.CollectAndWait();

            c.Clean();
            Assert.AreEqual(0, c.AddCountSinceLastClean);
            Assert.AreEqual(3, c.UnsafeCount);

            c.Remove(x);
            c.Remove(x);
            Assert.AreEqual(1, c.UnsafeCount);

            GC.KeepAlive(x);
        }
        public void TestRemoveObjectsAtSides()
        {
            var objects = new List <object>
            {
                new object(),
                new object(),
                new object(),
                new object(),
                new object(),
                new object(),
            };

            var list = new WeakList <object>();

            foreach (object o in objects)
            {
                list.Add(o);
            }

            list.Remove(objects[0]);
            list.Remove(objects[1]);
            list.Remove(objects[4]);
            list.Remove(objects[5]);

            Assert.That(list.Count(), Is.EqualTo(2));
            Assert.That(list, Does.Contain(objects[2]));
            Assert.That(list, Does.Contain(objects[3]));
        }
        public void TestIterateWithRemoveSkipsInvalidated()
        {
            object obj  = new object();
            object obj2 = new object();
            object obj3 = new object();

            var list = new WeakList <object> {
                obj, obj2, obj3
            };

            int count = 0;

            foreach (object item in list)
            {
                if (count == 0)
                {
                    list.Remove(obj2);
                }

                Assert.That(item, Is.Not.EqualTo(obj2));

                count++;
            }

            Assert.That(count, Is.EqualTo(2));

            GC.KeepAlive(obj);
            GC.KeepAlive(obj2);
            GC.KeepAlive(obj3);
        }
        public void TestIterateWithRemove()
        {
            object obj  = new object();
            object obj2 = new object();
            object obj3 = new object();

            var list = new WeakList <object> {
                obj, obj2, obj3
            };

            int count = 0;

            foreach (object item in list)
            {
                if (count == 1)
                {
                    list.Remove(item);
                }
                count++;
            }

            Assert.That(count, Is.EqualTo(3));

            Assert.That(list, Does.Contain(obj));
            Assert.That(list, Does.Not.Contain(obj2));
            Assert.That(list, Does.Contain(obj3));

            GC.KeepAlive(obj);
            GC.KeepAlive(obj2);
            GC.KeepAlive(obj3);
        }
        public void TestAddAfterRemoveFromEnd()
        {
            var objects = new List <object>
            {
                new object(),
                new object(),
                new object(),
            };

            object newLastObject = new object();

            var list = new WeakList <object>();

            foreach (object o in objects)
            {
                list.Add(o);
            }

            list.Remove(objects[2]);
            list.Add(newLastObject);

            Assert.That(list.Count(), Is.EqualTo(3));
            Assert.That(list, Does.Contain(objects[0]));
            Assert.That(list, Does.Contain(objects[0]));
            Assert.That(list, Does.Not.Contain(objects[2]));
            Assert.That(list, Does.Contain(newLastObject));
        }
Beispiel #6
0
        public void TestRemovedWeakReferencesAreNotContained()
        {
            var obj     = new object();
            var weakRef = new WeakReference <object>(obj);
            var list    = new WeakList <object> {
                weakRef
            };

            GC.TryStartNoGCRegion(10 * 1000000); // 10MB (should be enough)

            try
            {
                list.Remove(weakRef);
                Assert.That(list, Does.Not.Contain(weakRef));
            }
            finally
            {
                try
                {
                    GC.EndNoGCRegion();
                }
                catch
                {
                }
            }
        }
 /// <summary>
 /// Invoked by <see cref="ArchiveListSnapshot{TKey,TValue}.Dispose"/> method.
 /// </summary>
 /// <param name="archiveLists"></param>
 private void ReleaseClientResources(ArchiveListSnapshot <TKey, TValue> archiveLists)
 {
     lock (m_syncRoot)
     {
         m_allSnapshots.Remove(archiveLists);
     }
 }
Beispiel #8
0
        public void TestIterateWithRemoveSkipsInvalidated()
        {
            var obj  = new object();
            var obj2 = new object();
            var obj3 = new object();

            var list = new WeakList <object> {
                obj, obj2, obj3
            };

            int count = 0;

            foreach (var item in list)
            {
                if (count == 0)
                {
                    list.Remove(obj2);
                }

                Assert.AreNotEqual(obj2, item);

                count++;
            }

            Assert.AreEqual(2, count);
        }
Beispiel #9
0
 /// <summary>
 /// UnRegisters a client with the server host.
 /// </summary>
 private void UnRegisterClient(Client client)
 {
     lock (m_syncRoot)
     {
         m_clients.Remove(client);
     }
 }
Beispiel #10
0
 public void UnregisterEventHandler(EventHandler <TEventArgs> handler)
 {
     lock (weakReferences)
     {
         weakReferences.Clear(true);
         weakReferences.Remove(handler);
     }
 }
Beispiel #11
0
        public void Test()
        {
            Random rand = new Random(3);

            List <string>     list1 = new List <string>();
            WeakList <string> list2 = new WeakList <string>();

            for (int x = 0; x < 1000; x++)
            {
                string str = x.ToString();
                list1.Add(str);
                list2.Add(str);

                if (!list1.SequenceEqual(list2))
                {
                    throw new Exception("Lists are not the same.");
                }
            }

            for (int x = 1000; x < 2000; x++)
            {
                string str        = x.ToString();
                string removeItem = list1[rand.Next(list1.Count)];
                list1.Remove(removeItem);
                list2.Remove(removeItem);

                if (!list1.SequenceEqual(list2))
                {
                    throw new Exception("Lists are not the same.");
                }

                list1.Add(str);
                list2.Add(str);

                if (!list1.SequenceEqual(list2))
                {
                    throw new Exception("Lists are not the same.");
                }
            }

            for (int x = 0; x < 100; x++)
            {
                list1.RemoveAt(rand.Next(list1.Count));
                GC.Collect();

                if (!list1.SequenceEqual(list2))
                {
                    throw new Exception("Lists are not the same.");
                }
            }


            list2.Clear();
            foreach (string data in list2)
            {
                throw new Exception();
            }
        }
        public static void Remove(Drawable component)
        {
            if (!DebugUtils.IsDebugBuild)
            {
                return;
            }

            lock (loading_components)
                loading_components.Remove(component);
        }
        public void TestRemove()
        {
            object obj  = new object();
            var    list = new WeakList <object> {
                obj
            };

            list.Remove(obj);

            Assert.That(list.Count(), Is.Zero);
            Assert.That(list, Does.Not.Contain(obj));

            GC.KeepAlive(obj);
        }
        public void TestRemoveWeakReference()
        {
            object obj     = new object();
            var    weakRef = new WeakReference <object>(obj);
            var    list    = new WeakList <object> {
                weakRef
            };

            list.Remove(weakRef);

            Assert.That(list.Count(), Is.Zero);
            Assert.That(list.Contains(weakRef), Is.False);

            GC.KeepAlive(obj);
        }
        public void Test()
        {
            var rand = new Random(3);

            List<string> list1 = new List<string>();
            WeakList<string> list2 = new WeakList<string>();

            for (int x = 0; x < 1000; x++)
            {
                var str = x.ToString();
                list1.Add(str);
                list2.Add(str);

                if (!list1.SequenceEqual(list2))
                    throw new Exception("Lists are not the same.");
            }

            for (int x = 1000; x < 2000; x++)
            {
                var str = x.ToString();
                var removeItem = list1[rand.Next(list1.Count)];
                list1.Remove(removeItem);
                list2.Remove(removeItem);

                if (!list1.SequenceEqual(list2))
                    throw new Exception("Lists are not the same.");

                list1.Add(str);
                list2.Add(str);

                if (!list1.SequenceEqual(list2))
                    throw new Exception("Lists are not the same.");
            }

            for (int x = 0; x < 100; x++)
            {
                list1.RemoveAt(rand.Next(list1.Count));
                GC.Collect();

                if (!list1.SequenceEqual(list2))
                    throw new Exception("Lists are not the same.");
            }


            list2.Clear();
            foreach (var data in list2)
                throw new Exception();
        }
        protected override void OnDetaching()
        {
            DockLayoutManager manager = AssociatedObject as DockLayoutManager;

            if (manager != null && managers.Contains(manager))
            {
                lock (managers) {
                    foreach (var m in managers)
                    {
                        DockLayoutManagerLinker.Unlink(manager, m);
                    }
                    managers.Remove(manager);
                }
            }
            base.OnDetaching();
        }
Beispiel #17
0
        public void TestRemove()
        {
            var obj  = new object();
            var list = new WeakList <object> {
                obj
            };

            list.Remove(obj);

            int count = 0;

            foreach (var unused in list)
            {
                count++;
            }

            Assert.That(count, Is.Zero);
        }
Beispiel #18
0
        public void TestIterateWithRemove()
        {
            var obj  = new object();
            var obj2 = new object();
            var obj3 = new object();

            var list = new WeakList <object> {
                obj, obj2, obj3
            };

            int count = 0;

            foreach (var item in list)
            {
                if (count == 1)
                {
                    list.Remove(item);
                }
                count++;
            }

            Assert.AreEqual(3, count);
        }
Beispiel #19
0
 protected void Unbind(Bindable <T> binding) => Bindings.Remove(binding.weakReference);
Beispiel #20
0
 void OnStreamDisposal(SequentialReaderStream <TKey, TValue> stream)
 {
     m_openStreams.Remove(stream);
 }
 private void OnStreamDisposal(SequentialReaderStream <TKey, TValue> stream)
 {
     lock (m_syncRoot)
         m_openStreams.Remove(stream);
 }
 protected static void RemoveReference(Timer timer)
 {
     _runningTimers.Remove(timer);
 }