BADF00D / DisposableFixer

This is a Visual Studio Extension and NuGet package that should identify and fix problems as memleaks while using IDisposables.
Other
35 stars 7 forks source link

Using a tracking type/tracking method to dispose fields/properties initialized in constructor leads to a warning #124

Closed Claye-L closed 5 years ago

Claye-L commented 5 years ago

Prerequisites

Description

Initialize a IDisposable property or field in the constructor. Add it to a tracking type (CompositeDisposable here).Get a warning "Field/Property not disposed";

Source Code

 public class BaseCase : IDisposable
    {
        private MemoryStream field;
        public MemoryStream Property { get; set; }
        public BaseCase()
        {
            field = new MemoryStream(); //no warning as expected
            Property = new MemoryStream(); //no warning as expected
        }

        public void Dispose()
        {
            field.Dispose();
            Property.Dispose();
        }
    }

    public class WithTrackingMethod : IDisposable
    {
        private MemoryStream field;
        public MemoryStream Property { get; set; }
        private CompositeDisposable disposables;
        public WithTrackingMethod()
        {
            disposables = new CompositeDisposable(); //no warning as expected
            field = new MemoryStream(); //Warning : Field not disposed, should not be
            Property = new MemoryStream(); //Warning : Property not disposed, should not be
            var local = new MemoryStream(); //no warning as expected

            disposables.Add(local);
            disposables.Add(field);
            disposables.Add(Property);
        }
        public void Dispose()
        {
            disposables.Dispose();
        }
    }
BADF00D commented 5 years ago

@Claye-L I evaluated the problem last weekend and was able to fix it. I detected similar situation that causes the same problem and tries to fix them, too. Unfortunately all my testrunners went nuts, so I was not able to finish the ticket yet.

BADF00D commented 5 years ago

Will be part of next release