ScriptObject.cs 1.7 KB

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