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

Analyzer throwing NullReferenceException during build. #64

Closed ericwra closed 7 years ago

ericwra commented 7 years ago

Prerequisites

Description

Some projects cause DisposableFixer to throw a System.NullReferenceException on Build.

Analyzer 'DisposableFixer.DisposableFixerAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.

Source Code

I have not been able to isolate this problem outside of my very large solution. If I recreate the project it is throwing on outside of the large solution it seems to have no issues during build.

Screenshot

BADF00D commented 7 years ago

Thanks for reporting the error. Can you make another screenshot, where I can see the stacktrace? Simply click on the small triangle in front of each line. I just inserted a NotImplmentedException in another extension to demonstrate this: image

BADF00D commented 7 years ago

I just saw you are running version 0.12. This is one of the first version I published. There are lots of known bugs. The current version 0.30 might already solve your problem.

You might have downloaded the last released published on github. But its also available on Visualstudio gallery, including autoupdate

ericwra commented 7 years ago

I was mistaken about the version number. I have the latest from the visual studios marketplace / gallery version 0.30.

After further examination of the stack traces I've been able to determine there was indeed a IDisposable not being disposed of in each of the error locations. They were all simple declarations like the instance below.

I've attached a screenshot with the stacktrace as requested.

Line:25 -> var db = new EmailEntities();

changed to using(var db = new EmailEntities()) { blah blah... etc.. }

BADF00D commented 7 years ago

Hi, unfortunately I was not able to reproduce the problem, yet. But I refactored the code and split the method into several once. I published version 0.31 with this changes.

Can you once more try to reproduce the problem and send me a screenshot of the new stacktrace?

ericwra commented 7 years ago

I've updated to 0.31 and was able to trigger the same exception as before.

BADF00D commented 7 years ago

Thanks for the new stacktrace.

ericwra commented 7 years ago

No problem, let me know if there is anything I can do to help you track this down. I find this extension very useful when refactoring large code bases.

BADF00D commented 7 years ago

I think I finally found the problem. Do you use property initializers? Something like:

using System;
using System.IO;
namespace SomeNamespace{
    internal class Program {
        private static void Main(string[] args) {
            var memoryStream = new MemoryStream();
            new Foo {Prop = memoryStream};//this causes the problem
        }
    }

    internal class Foo {
        public IDisposable Prop { get; set; }
        public Foo() { }
    }
}

Can you please add the optional braces and check if the error is gone?

using System;
using System.IO;
namespace SomeNamespace{
    internal class Program {
        private static void Main(string[] args) {
            var memoryStream = new MemoryStream();
            new Foo (){Prop = memoryStream};//should be fixed with braces
        }
    }

    internal class Foo {
        public IDisposable Prop { get; set; }
        public Foo() { }
    }
}

I thought that each constructor should have a (maybe empty) ArgumentList, but when using property initializer the ArgumentList is actually null, instead of empty.

If you can verify the solution, the fix should be very easy.

ericwra commented 7 years ago

That fixed the issue. Thanks for tracking down this exception!

BADF00D commented 7 years ago

By the way, which version of Visual Studio are you using? 2015/2017, Community Edition, Professional, Enterprise? I'm asking because currently I wasn't able to spot this problem on my own.

BADF00D commented 7 years ago

Is part of release 0.32