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

Undisposed local variable within Func #90

Closed BADF00D closed 6 years ago

BADF00D commented 6 years ago

Prerequisites

Description

An undisposed local variable within a func that is created as parameter in a MethodInvocation is not marked as disposed, when the result of the MethodInvocation is return directly. If the result is stoed in another local variable, the undisposed local variable is correctly marked as not disposed.

Source Code

Incorrect: Not marked as disposed:

namespace MyNamespace {
    public class Dummy {
        public object Create() {
            return Do(
                () => {
                    var memoryStream = new System.IO.MemoryStream();

                    return 0;
                });
        }

        private IDisposable Do(Func<int> someFunc) {
            return new MemoryStream();
        }
    }
}

image

Correct:

namespace MyNamespace {
    public class Dummy {
        public object Create() {
            var x = Do(
                () => {
                    var memoryStream = new System.IO.MemoryStream();

                    return 0;
                });

            return x;
        }

        private IDisposable Do(Func<int> someFunc) {
            return new MemoryStream();
        }
    }
}

image

BADF00D commented 6 years ago

Will be part of release 1.1.1