Skip to content

linksplatform/Disposables

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NuGet Version and Downloads count Actions Status Codacy Badge CodeFactor

This library helps to make objects disposable in a fast, short, easy and safe way. Say goodbye to memory leaks one at time :)

Platform.Disposables.DisposableBase abstract class tries to dispose the object at both on instance destruction and OnProcessExit whatever comes first even if Dispose method was not called anywhere by user. And takes care your disposal logic executes only once by default, and if you really need it, you can allow multiple calls and attempts to dispose, by overriding corresponding properties.

The Platform.Disposables.IDisposable interface extends the System.IDisposable with IsDisposed property and Destruct method. The Destruct method is designed to never throw exceptions, that makes it safe to use in class destructors. All ignored exceptions are available at Platform.Disposables.IgnoredDisposables if you need to debug them.

Namespace: Platform.Disposables

Forked from: Konard/LinksPlatform/Platform/Platform.Helpers/Disposables

Package at NuGet: Platform.Disposables

Examples

If you can use inheritance in your class. For example if you don't have other base class inherited.

using Platform.Disposables;

namespace Examples
{
    public class DisposableBaseUsageExample : DisposableBase
    {
        protected override void Dispose(bool manual, bool wasDisposed)
        {
            // Put your dispose logic in this method

            if (!wasDisposed) 
            {
                // First call from Dispose or Destructor
            }
            else 
            {
                // (IsDisposed == true) here, Dispose called twise, or Dispose and Destructor were called

                if (manual)
                {
                    // Call from IDisposable.Dispose()
                }
                else
                {
                    // Call from Destructor
                }
            }
        }
    }
}

If you cannot use inheritance in your class. For example if you have other base class inherited.

using Platform.Disposables;

namespace Examples
{
    public class DisposableUsageExample : System.IDisposable
    {
        private readonly Disposable _disposable;

        public DisposableUsageExample() => _disposable = new Disposable(Disposed);

        public void Dispose() => _disposable.Dispose();

        ~DisposableUsageExample() => _disposable.Destruct();

        private void Disposed(bool manual, bool wasDisposed)
        {
            // Dispose logic
        }
    }
}

If you do not have access to the internal structure of the object's class. You can use a disposable object container. The container is convertible back to the passed object at any time.

using Platform.Disposables;

namespace Examples
{
    public class Examples
    {
        public static void UseAndDispose()
        {
            var array = new int[] { 1, 2, 3 };
            Disposable<int[]> disposableArray = (array, () => array = null);
            WorkWithObjectAndDispose(disposableArray);
            // Here array == null
        }

        private static void WorkWithObjectAndDispose(Disposable<int[]> disposableArray)
        {
            using (disposableArray)
            {
                int[] array = disposableArray;

                // Use your object here

            } // Here there is call to () => array = null
        }
    }
}

PDF file with code for e-readers.

Depend on

Dependent libraries