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

Add support for Interlocked.Exchange #106

Closed BADF00D closed 5 years ago

BADF00D commented 5 years ago

Prerequisites

Description

When using Interlocked.Exchange with an IDisposable instance, my IDisposable is tracked by the referenced field. If this is a field or property, then my IDisposable is tracked by this member. So there should be no hint, that the field disposable is not disposed.

Source Code

The following code shows an example for this kind of member assignment:

using System;
using System.IO;
using System.Threading;

namespace RxTimeoutTest
{
    internal class SomeClass : IDisposable
    {
        private IDisposable _field;

        public void Exchange()
        {
            var mem = new MemoryStream();

            Interlocked.Exchange(ref _field, mem)
                ?.Dispose();
        }

        public void Dispose()
        {
            _field?.Dispose();
        }
    }
}

Where the following code should yield a warning:

using System;
using System.IO;
using System.Threading;

namespace RxTimeoutTest
{
    internal class SomeClass
    {
        private IDisposable _field;

        public void Exchange()
        {
            var mem = new MemoryStream();

            Interlocked.Exchange(ref _field, mem)
                ?.Dispose();
        }
    }
}

Screenshot

image

BADF00D commented 5 years ago

There are mote test required: //Creation

BADF00D commented 5 years ago

Will be part of release 1.5.0