ScriptObject.cs 1.7 KB

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