ScriptObject.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// A base class for all script objects that interface with the native code.
  7. /// </summary>
  8. public class ScriptObject
  9. {
  10. /// <summary>
  11. /// A pointer to the native script interop object.
  12. /// </summary>
  13. internal IntPtr mCachedPtr;
  14. /// <summary>
  15. /// Notifies the native script interop object that the managed instance was finalized.
  16. /// </summary>
  17. ~ScriptObject()
  18. {
  19. if (mCachedPtr == IntPtr.Zero)
  20. {
  21. Debug.LogError("Script object is being finalized but doesn't have a pointer to its interop object. Type: " + GetType());
  22. #if DEBUG
  23. // This will cause a crash, so we ignore it in release mode hoping all it causes is a memory leak.
  24. Internal_ManagedInstanceDeleted(mCachedPtr);
  25. #endif
  26. }
  27. else
  28. Internal_ManagedInstanceDeleted(mCachedPtr);
  29. }
  30. /// <summary>
  31. /// Returns a pointer to the native script interop object.
  32. /// </summary>
  33. /// <returns>Pointer to the native script interop object</returns>
  34. internal IntPtr GetCachedPtr()
  35. {
  36. return mCachedPtr;
  37. }
  38. [MethodImpl(MethodImplOptions.InternalCall)]
  39. private static extern void Internal_ManagedInstanceDeleted(IntPtr nativeInstance);
  40. }
  41. }