AceLand Unity Packages
  • Home
  • Getting Started
    • Installation
    • Project Settings
    • Architecture Graph
    • Development Level
  • Tutorial
    • Create Your Package
    • Create Project Settings
  • Packages
    • Library
      • Change Log
      • Editor Tools
      • Mono
        • Follow Object
        • Singleton
      • Attributes
        • Conditional Show
        • Inspector Button
        • ReadOnly Field
      • Build Leveling
      • CVS
      • DataTools
      • Disposable Object
      • Extensions
      • Json
      • Kalman Filter
      • Optional
      • Project Setting
      • Serialization Surrogate
      • Utils
    • Event Driven
      • Change Log
      • Event Bus
      • Signal
    • Input
      • Change Log
    • Memento Service
      • Change Log
    • Node Framework
      • Change Log
      • Mono Node
    • Node For Mono (dev)
    • Player Loop Hack
      • Change Log
    • Pool
      • Change Log
    • Security (dev)
    • States
      • Change Log
    • Task Utils
      • Change Log
    • WebRequest
      • Change Log
Powered by GitBook
On this page
  • Features
  • Usage Example
  • Override DisposeManagedResources
  • Override DisposeUnmanagedResources
  • Best Practices
  • Important Considerations
  1. Packages
  2. Library

Disposable Object

A base class that implements the IDisposable pattern correctly

Disposable Object ensures proper cleanup of both managed and unmanaged resources. It provides a structured way to handle resource disposal and helps prevent memory leaks in your Unity applications.

Features

  • Automatic disposal state tracking

  • Separate handling for managed and unmanaged resources

  • Protection against multiple disposals

  • Proper implementation of IDisposable pattern

  • GC optimization through SuppressFinalize


Usage Example

public class MyResourceHandler : DisposableObject
{
    private FileStream _fileStream;  // Managed resource
    private IntPtr _nativeHandle;    // Unmanaged resource

    protected override void DisposeManagedResources()
    {
        // Dispose managed resources
        _fileStream?.Dispose();
    }

    protected override void DisposeUnmanagedResources()
    {
        // Clean up unmanaged resources
        if (_nativeHandle != IntPtr.Zero)
        {
            Marshal.FreeHGlobal(_nativeHandle);
            _nativeHandle = IntPtr.Zero;
        }
    }
}

// Using the class
using (var handler = new MyResourceHandler())
{
    // Work with resources
}  // Automatically disposes when leaving scope

Override DisposeManagedResources

Override this method to clean up managed resources:

protected override void DisposeManagedResources()
{
    // Dispose managed objects that implement IDisposable
    _disposableField?.Dispose();
    _listField?.Clear();
}

Override DisposeUnmanagedResources

Override this method to clean up unmanaged resources:

protected override void DisposeUnmanagedResources()
{
    // Clean up unmanaged resources like:
    // - Native memory allocations
    // - File handles
    // - System handles
}

Best Practices

  • Always use using statements when possible

  • Check Disposed property before operations:

public void DoSomething()
{
    if (Disposed) throw new ObjectDisposedException(nameof(MyResourceHandler));
    // Method implementation
}
  • Don't call base methods when overriding:

protected override void DisposeManagedResources()
{
    // Don't call base.DisposeManagedResources()
    // Just implement your cleanup
}
  • Handle disposal in a deterministic manner:

public void CleanupResources()
{
    if (!Disposed)
    {
        Dispose();
    }
}

Important Considerations

  • Override DisposeManagedResources for IDisposable objects

  • Override DisposeUnmanagedResources for native resources

  • Don't throw exceptions from Dispose methods

  • Always check Disposed state in public methods

  • Use try-finally blocks for critical cleanup operations


Last updated 4 months ago