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 an extension method to wrap CompositeDisposable.Add() triggers warning when it shouldnt #120

Closed Claye-L closed 5 years ago

Claye-L commented 5 years ago

Prerequisites

Description

I use ReactiveUI, which uses an extension method DisposeWith() that is just a wrapper for CompositeDisposable.Add(). Calling this method does not diable the warning on the disposable object.

I have seen #34 and #49 that already reference this problem, however i can not find the AddTo extension method that you refer to in #34 ( Reactive.Bindings.Extensions.IDisposableExtensions.AddTo(this T disposable, ICollection container) where T : Disposable), as that method seems to not be present in the Reactive source code.

Source Code

//this does not trigger a warning
var stream0 = new MemoryStream();
var composite0 = new CompositeDisposable();
composite0.Add(stream0);
composite0.Dispose();

//this does trigger a warning even though all the objects are correctly disposed

var stream1 = new MemoryStream(); //Warning: Local variable not disposed
var composite1 = new CompositeDisposable();
//both implementations of a simple wrapper around Add() dont make the warning go away
stream1.DispWith(composite1);
stream1.AddTo(composite1);

composite1.Dispose();
public static class Mixin
    {
// this method is equivalent to reactiveui's DisposeWith
        public static T DispWith<T>(this T item,CompositeDisposable disp) where T:IDisposable
        {
            disp.Add(item);
            return item;
        }
// this method is an alternate implementation 
        public static void AddTo<T>(this T item, CompositeDisposable disp) where T : IDisposable
        {
            disp.Add(item);
        }
    }

This feature would make this extension a great tool when working with reactiveui or reactive extensions in general as this kind of work generates a lot of disposable objects.

Suggested solution

Add tracking for reactiveui's DisposeWith() extension method, which acts as a replacement for the AddTo() extension method.

BADF00D commented 5 years ago

Hi, first of all, thank for reporting. The AddTo-Method is from ReactiveProperty (a alternative to ReactiveUI). It's an example of what I call a tracking method. Since this concept is already build-in the extension (from the bug in #34), all I need Is the signature and full qualified namespace of the method DisposeWith . Looks like you provided this with your last link.

System.Reactive.Disposables.DisposableMixins.DisposeWith<T>(this T item, CompositeDisposable compositeDisposable)

Since ReactiveUI is a open source library, I'm going to add this to the default configuration. You can see the current default configuration here

Currently the configuration is build-in an not adjustable. Its planed to make it configurable within Visual Studio in future.

BADF00D commented 5 years ago

Will be part of release 2.0.0