leakHunter.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2013 Michael Zeilfelder
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef IRR_LEAK_HUNTER_INCLUDED
  5. #define IRR_LEAK_HUNTER_INCLUDED
  6. #include "IrrCompileConfig.h"
  7. #ifdef _IRR_COMPILE_WITH_LEAK_HUNTER_
  8. #include "irrArray.h"
  9. namespace irr
  10. {
  11. class IReferenceCounted;
  12. //! A class helping to find unreleased objects of type IReferenceCounted.
  13. /** To use this you have recompile Irrlicht with _IRR_COMPILE_WITH_LEAK_HUNTER_.
  14. Note that this will slow down your application and should only be used for debugging.
  15. The way to use is that you can check after you closed and dropped your last Irrlicht device
  16. if there are still any IReferenceCounted left over which have not been deleted.
  17. */
  18. class LeakHunter
  19. {
  20. public:
  21. friend class IReferenceCounted;
  22. //! Clear all IReferenceCounted objects inside LeakHunter
  23. /** This does not affect the IReferenceCounted themselves only the
  24. counting of them. Usually you don't ever need to clear, but
  25. sometimes it helps when for example you want to ignore
  26. certain leaks.
  27. */
  28. static void clearReferenceCountedObjects()
  29. {
  30. ReferenceCountedObjects.clear();
  31. }
  32. //! Access all objects which are currently reference counted.
  33. static inline irr::core::array<const IReferenceCounted*> getReferenceCountedObjects()
  34. {
  35. return ReferenceCountedObjects;
  36. }
  37. protected:
  38. static inline void addObject(const IReferenceCounted* object)
  39. {
  40. ReferenceCountedObjects.push_back(object);
  41. }
  42. static inline void removeObject(const IReferenceCounted* object)
  43. {
  44. irr::s32 idx = ReferenceCountedObjects.linear_search(object );
  45. if ( idx >= 0 )
  46. {
  47. irr::core::swap( ReferenceCountedObjects[idx], ReferenceCountedObjects.getLast() );
  48. ReferenceCountedObjects.erase( ReferenceCountedObjects.size()-1 );
  49. }
  50. }
  51. private:
  52. // NOTE: We don't do additional grab()/drop()'s here as we want to supervise reference counted objects and not affect them otherwise.
  53. IRRLICHT_API static irr::core::array<const IReferenceCounted*> ReferenceCountedObjects;
  54. };
  55. } // end namespace irr
  56. #endif // _IRR_COMPILE_WITH_LEAK_HUNTER_
  57. #endif