DynamicData icon indicating copy to clipboard operation
DynamicData copied to clipboard

Wrappers generated by Transform are reused after being disposed

Open mikefayer opened this issue 11 years ago • 4 comments

If a given type is being directed to different observable collections based on some filter, and with a transform, and it moves from one collection to another and then back, then the wrapper type is no longer disposed and recreated. On the last move, it's disposed and reused, leading to Bad Things. The test below fails:

using System;
using DynamicData;
using DynamicData.Binding;
using DynamicData.Controllers;
using NUnit.Framework;

namespace DDataTest {
    public class Widget {
        public int Id { get; set; }
        public bool Active { get; set; }
    }

    public class WidgetWrapper : IDisposable {
        public bool IsDisposed { get; private set; }
        public Widget Wrapped { get; private set; }
        public WidgetWrapper(Widget Target) {
            IsDisposed = false;
            Wrapped = Target;
        }

        public void Dispose() {
            IsDisposed = true;
        }
    }

    public class ThrowawayTest {

        [Test]
        public void Does_Not_Reuse_Disposed_Wrappers() {
            var subject = new SourceCache<Widget, int>(w => w.Id);

            var activeObservable = new ObservableCollectionExtended<WidgetWrapper>();
            var inactiveObservable = new ObservableCollectionExtended<WidgetWrapper>();

            var activeFilter = new FilterController<WidgetWrapper>(wrapper => wrapper.Wrapped.Active);
            var inactiveFilter = new FilterController<WidgetWrapper>(wrapper => !wrapper.Wrapped.Active);

            subject.Connect()
                   .Transform(w => new WidgetWrapper(w))
                   .Filter(activeFilter)
                   .Bind(activeObservable)
                   .DisposeMany()
                   .Subscribe();

            subject.Connect()
                   .Transform(w => new WidgetWrapper(w))
                   .Filter(inactiveFilter)
                   .Bind(inactiveObservable)
                   .DisposeMany()
                   .Subscribe();

            var w1 = new Widget() { Id = 0, Active = true };
            var w2 = new Widget() { Id = 1, Active = true };

            subject.AddOrUpdate(w1);
            subject.AddOrUpdate(w2);
            Assert.AreEqual(2, activeObservable.Count);
            Assert.AreEqual(0, inactiveObservable.Count);
            Assert.IsFalse(activeObservable[0].IsDisposed);
            Assert.IsFalse(activeObservable[1].IsDisposed);

            //This needs to be done twice to trigger the behavior
            w1.Active = !w1.Active;
            activeFilter.Reevaluate();
            inactiveFilter.Reevaluate();

            w1.Active = !w1.Active;
            activeFilter.Reevaluate();
            inactiveFilter.Reevaluate();

            Assert.AreEqual(2, activeObservable.Count);

            Assert.False(activeObservable[0].IsDisposed);
            Assert.False(activeObservable[1].IsDisposed);
        }

    }
}

mikefayer avatar Nov 21 '15 02:11 mikefayer

This is 100% by design and is intended to prevent memory leaks.

But as ever with dynamic data there is a fix

        [Test]
        public void Does_Not_Reuse_Disposed_Wrappers_FIXED()
        {
            var subject = new SourceCache<Widget, int>(w => w.Id);

            var activeObservable = new ObservableCollectionExtended<WidgetWrapper>();
            var inactiveObservable = new ObservableCollectionExtended<WidgetWrapper>();

            var activeFilter = new FilterController<WidgetWrapper>(wrapper => wrapper.Wrapped.Active);
            var inactiveFilter = new FilterController<WidgetWrapper>(wrapper => !wrapper.Wrapped.Active);

        //Items are only disposed when removed from 'subject' 
        //or when converted.Dispose() is called 
            var converted = subject
                .Connect()
                .Transform(w => new WidgetWrapper(w))
                .DisposeMany()
                .AsObservableCache();

            var active = converted.Connect()
                   .Filter(activeFilter)
                   .Bind(activeObservable)
                   .Subscribe();

            var inactive = converted.Connect()
                   .Filter(inactiveFilter)
                   .Bind(inactiveObservable)
                   .Subscribe();

            var w1 = new Widget() { Id = 0, Active = true };
            var w2 = new Widget() { Id = 1, Active = true };

            subject.AddOrUpdate(w1);
            subject.AddOrUpdate(w2);
            Assert.AreEqual(2, activeObservable.Count);
            Assert.AreEqual(0, inactiveObservable.Count);
            Assert.IsFalse(activeObservable[0].IsDisposed);
            Assert.IsFalse(activeObservable[1].IsDisposed);

            //This needs to be done twice to trigger the behavior
            w1.Active = !w1.Active;
            activeFilter.Reevaluate();
            inactiveFilter.Reevaluate();

            w1.Active = !w1.Active;
            activeFilter.Reevaluate();
            inactiveFilter.Reevaluate();

            Assert.AreEqual(2, activeObservable.Count);

            Assert.False(activeObservable[0].IsDisposed);
            Assert.False(activeObservable[1].IsDisposed);
        }

Also make sure you dispose all the caches / and subscribed to objects when they are finished with

RolandPheasant avatar Nov 21 '15 05:11 RolandPheasant

Thanks, your workaround fixed my problem. But, this was an easy hole to fall into and I don't really see that I did anything glaringly wrong, perhaps there needs to be some red flags for others somewhere. Then again, I'm a newbie to this library and may just be misunderstanding it right now.

mikefayer avatar Nov 23 '15 16:11 mikefayer

@tempywritescode Without doubt I need to create some good guidelines. However the permutations are endless, just like Rx so there is so there is no way I would ever be able to cover everything up front.

The key in this instance is DisposeOnRemove disposes any thing which is reported to be removed, so when a filter changes it is removed from one of the collections.

Feel free to ask any question. The dynamic data Gitter page is normally the best point of contact

RolandPheasant avatar Nov 23 '15 16:11 RolandPheasant

I had the same problem while doing a sort. after a sort my items where disposed wheareas they never really left the collection. This fix should be on the main example page. it's very easy to make this mistake and no one will thougth DynamicData has dispose the items when they didn't left the collection from the point of view of the user.

valhentai avatar May 23 '19 14:05 valhentai