using System; using System.Runtime.CompilerServices; namespace BansheeEngine { /// /// A base class for all script objects that interface with the native code. /// public class ScriptObject { /// /// A pointer to the native script interop object. /// internal IntPtr mCachedPtr; /// /// Notifies the native script interop object that the managed instance was finalized. /// ~ScriptObject() { if (mCachedPtr == IntPtr.Zero) { Debug.LogError("Script object is being finalized but doesn't have a pointer to its interop object. Type: " + GetType()); #if DEBUG // This will cause a crash, so we ignore it in release mode hoping all it causes is a memory leak. Internal_ManagedInstanceDeleted(mCachedPtr); #endif } else Internal_ManagedInstanceDeleted(mCachedPtr); } /// /// Returns a pointer to the native script interop object. /// /// Pointer to the native script interop object internal IntPtr GetCachedPtr() { return mCachedPtr; } [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_ManagedInstanceDeleted(IntPtr nativeInstance); } }