From 433445cdc226c9377245724b67d79275a435845f Mon Sep 17 00:00:00 2001 From: cutealien Date: Thu, 3 Jan 2013 17:24:30 +0000 Subject: [PATCH] Add a LeakHunter class which can be enabled with compile-flag _IRR_COMPILE_WITH_LEAK_HUNTER_ to find leaking IReferenceCounted objects. Will break OSX compiling for now as that project file is not yet updated. I hope other project files are all fixed correctly. git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@4425 dfc29bdd-3216-0410-991c-e03cc46cb475 --- changes.txt | 1 + include/IReferenceCounted.h | 10 +++ include/IrrCompileConfig.h | 7 ++ include/leakHunter.h | 68 ++++++++++++++++++++ source/Irrlicht/Irrlicht-gcc.cbp | 2 + source/Irrlicht/Irrlicht10.0.vcxproj | 1 + source/Irrlicht/Irrlicht10.0.vcxproj.filters | 3 + source/Irrlicht/Irrlicht11.0.vcxproj | 2 + source/Irrlicht/Irrlicht11.0.vcxproj.filters | 6 ++ source/Irrlicht/Irrlicht8.0.vcproj | 8 +++ source/Irrlicht/Irrlicht9.0.vcproj | 8 +++ source/Irrlicht/Irrlicht_mobile6.vcproj | 8 +++ source/Irrlicht/Irrlicht_xbox.vcproj | 6 ++ source/Irrlicht/Makefile | 2 +- source/Irrlicht/leakHunter.cpp | 15 +++++ 15 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 include/leakHunter.h create mode 100644 source/Irrlicht/leakHunter.cpp diff --git a/changes.txt b/changes.txt index 513418d4..7b4dceef 100644 --- a/changes.txt +++ b/changes.txt @@ -1,6 +1,7 @@ -------------------------- Changes in 1.9 (not yet released) +- Add a LeakHunter class which can be enabled with compile-flag _IRR_COMPILE_WITH_LEAK_HUNTER_ to find leaking IReferenceCounted objects. - Add _IRR_COMPILE_WITH_XML_ define to allow compiling Irrlicht without xml (patch written by curaga) - Add functions to set/get cursor character and blinktime to IGUIEditBox - Collada exporter calculates values for orthographic camera now on export diff --git a/include/IReferenceCounted.h b/include/IReferenceCounted.h index 8a551e8c..1f042430 100644 --- a/include/IReferenceCounted.h +++ b/include/IReferenceCounted.h @@ -7,6 +7,10 @@ #include "irrTypes.h" +#ifdef _IRR_COMPILE_WITH_LEAK_HUNTER_ + #include "leakHunter.h" +#endif + namespace irr { @@ -46,11 +50,17 @@ namespace irr IReferenceCounted() : DebugName(0), ReferenceCounter(1) { +#ifdef _IRR_COMPILE_WITH_LEAK_HUNTER_ + LeakHunter::addObject(this); +#endif } //! Destructor. virtual ~IReferenceCounted() { + #ifdef _IRR_COMPILE_WITH_LEAK_HUNTER_ + LeakHunter::removeObject(this); + #endif } //! Grabs the object. Increments the reference counter by one. diff --git a/include/IrrCompileConfig.h b/include/IrrCompileConfig.h index 2a2f4fa0..82f0f6b9 100644 --- a/include/IrrCompileConfig.h +++ b/include/IrrCompileConfig.h @@ -122,6 +122,13 @@ #undef _IRR_COMPILE_WITH_XML_ #endif +//! Add a leak-hunter to Irrlicht which helps finding unreleased reference counted objects. +//! NOTE: This is slow and should only be used for debugging +//#define _IRR_COMPILE_WITH_LEAK_HUNTER_ +#ifdef NO_IRR_COMPILE_WITH_LEAK_HUNTER_ +#undef _IRR_COMPILE_WITH_LEAK_HUNTER_ +#endif + //! Define _IRR_COMPILE_WITH_DIRECT3D_8_ and _IRR_COMPILE_WITH_DIRECT3D_9_ to //! compile the Irrlicht engine with Direct3D8 and/or DIRECT3D9. /** If you only want to use the software device or opengl you can disable those defines. diff --git a/include/leakHunter.h b/include/leakHunter.h new file mode 100644 index 00000000..913424b4 --- /dev/null +++ b/include/leakHunter.h @@ -0,0 +1,68 @@ +// Copyright (C) 2013 Michael Zeilfelder +// This file is part of the "Irrlicht Engine". +// For conditions of distribution and use, see copyright notice in irrlicht.h + +#ifndef __LEAK_HUNTER_INCLUDEED__ + +#include "IrrCompileConfig.h" + +#ifdef _IRR_COMPILE_WITH_LEAK_HUNTER_ + +#include "irrArray.h" + +namespace irr +{ + class IReferenceCounted; + + //! A calls helping to find unrelease objects of type IReferenceCounted. + /** To use this you have recompile Irrlicht with _IRR_COMPILE_WITH_LEAK_HUNTER_. + Note that this will slow down your application and should only be used for debugging. + The way to use is that you can check after you closed and dropped your last Irrlicht device + if there are still any IReferenceCounted left over which have not been deleted. + */ + class LeakHunter + { + public: + friend class IReferenceCounted; + + //! Clear all IReferenceCounted objects inside LeakHunter + /** This does not affect the IReferenceCounted themselfes only the + counting of them. Usually you don't ever need to clear, but + sometimes it helps when for example you want for to ignore + certain leaks. + */ + static void clearReferenceCountedObjects() + { + ReferenceCountedObjects.clear(); + } + + static inline irr::core::array getReferenceCountedObjects() + { + return ReferenceCountedObjects; + } + + protected: + static inline void addObject(const IReferenceCounted* object) + { + ReferenceCountedObjects.push_back(object); + } + + static inline void removeObject(const IReferenceCounted* object) + { + irr::s32 idx = ReferenceCountedObjects.linear_search(object ); + if ( idx >= 0 ) + { + irr::core::swap( ReferenceCountedObjects[idx], ReferenceCountedObjects.getLast() ); + ReferenceCountedObjects.erase( ReferenceCountedObjects.size()-1 ); + } + } + + private: + // NOTE: We don't do additional grab()/drop()'s here as we want to supervise reference counted objects and not affect them otherwise. + IRRLICHT_API static irr::core::array ReferenceCountedObjects; + }; +} // end namespace irr + +#endif // _IRR_COMPILE_WITH_LEAK_HUNTER_ + +#endif diff --git a/source/Irrlicht/Irrlicht-gcc.cbp b/source/Irrlicht/Irrlicht-gcc.cbp index 073b4099..ea6f863f 100644 --- a/source/Irrlicht/Irrlicht-gcc.cbp +++ b/source/Irrlicht/Irrlicht-gcc.cbp @@ -591,6 +591,7 @@ + @@ -1273,6 +1274,7 @@ + diff --git a/source/Irrlicht/Irrlicht10.0.vcxproj b/source/Irrlicht/Irrlicht10.0.vcxproj index ce22b5e4..b97e3e7f 100644 --- a/source/Irrlicht/Irrlicht10.0.vcxproj +++ b/source/Irrlicht/Irrlicht10.0.vcxproj @@ -1407,6 +1407,7 @@ + diff --git a/source/Irrlicht/Irrlicht10.0.vcxproj.filters b/source/Irrlicht/Irrlicht10.0.vcxproj.filters index fa2ee210..f669f041 100644 --- a/source/Irrlicht/Irrlicht10.0.vcxproj.filters +++ b/source/Irrlicht/Irrlicht10.0.vcxproj.filters @@ -2243,6 +2243,9 @@ Irrlicht\video + + Irrlicht\irr + diff --git a/source/Irrlicht/Irrlicht11.0.vcxproj b/source/Irrlicht/Irrlicht11.0.vcxproj index bd821ff7..969a9865 100644 --- a/source/Irrlicht/Irrlicht11.0.vcxproj +++ b/source/Irrlicht/Irrlicht11.0.vcxproj @@ -848,6 +848,7 @@ + @@ -1414,6 +1415,7 @@ + diff --git a/source/Irrlicht/Irrlicht11.0.vcxproj.filters b/source/Irrlicht/Irrlicht11.0.vcxproj.filters index c522cec4..104d8e4d 100644 --- a/source/Irrlicht/Irrlicht11.0.vcxproj.filters +++ b/source/Irrlicht/Irrlicht11.0.vcxproj.filters @@ -122,6 +122,9 @@ include + + + include include @@ -1807,6 +1810,9 @@ Irrlicht\irr + + + Irrlicht\irr Irrlicht\irr\extern diff --git a/source/Irrlicht/Irrlicht8.0.vcproj b/source/Irrlicht/Irrlicht8.0.vcproj index 38ce04b3..9499ad64 100644 --- a/source/Irrlicht/Irrlicht8.0.vcproj +++ b/source/Irrlicht/Irrlicht8.0.vcproj @@ -606,6 +606,10 @@ RelativePath=".\..\..\include\irrlicht.h" > + + @@ -2981,6 +2985,10 @@ RelativePath="os.cpp" > + + diff --git a/source/Irrlicht/Irrlicht9.0.vcproj b/source/Irrlicht/Irrlicht9.0.vcproj index 610fbcfa..410de79c 100644 --- a/source/Irrlicht/Irrlicht9.0.vcproj +++ b/source/Irrlicht/Irrlicht9.0.vcproj @@ -705,6 +705,10 @@ RelativePath="..\..\include\irrlicht.h" > + + @@ -2700,6 +2704,10 @@ RelativePath="Irrlicht.cpp" > + + diff --git a/source/Irrlicht/Irrlicht_mobile6.vcproj b/source/Irrlicht/Irrlicht_mobile6.vcproj index 5a400c0c..7c578edf 100644 --- a/source/Irrlicht/Irrlicht_mobile6.vcproj +++ b/source/Irrlicht/Irrlicht_mobile6.vcproj @@ -349,6 +349,10 @@ RelativePath="..\..\include\irrlicht.h" > + + @@ -2554,6 +2558,10 @@ RelativePath="os.cpp" > + + diff --git a/source/Irrlicht/Irrlicht_xbox.vcproj b/source/Irrlicht/Irrlicht_xbox.vcproj index d92d0dfd..116e5f05 100644 --- a/source/Irrlicht/Irrlicht_xbox.vcproj +++ b/source/Irrlicht/Irrlicht_xbox.vcproj @@ -516,6 +516,9 @@ + + @@ -1419,6 +1422,9 @@ + + diff --git a/source/Irrlicht/Makefile b/source/Irrlicht/Makefile index 5a867e2a..0173e12f 100644 --- a/source/Irrlicht/Makefile +++ b/source/Irrlicht/Makefile @@ -44,7 +44,7 @@ IRRIMAGEOBJ = CColorConverter.o CImage.o CImageLoaderBMP.o CImageLoaderDDS.o CIm IRRVIDEOOBJ = CVideoModeList.o CFPSCounter.o $(IRRDRVROBJ) $(IRRIMAGEOBJ) IRRSWRENDEROBJ = CSoftwareDriver.o CSoftwareTexture.o CTRFlat.o CTRFlatWire.o CTRGouraud.o CTRGouraudWire.o CTRNormalMap.o CTRStencilShadow.o CTRTextureFlat.o CTRTextureFlatWire.o CTRTextureGouraud.o CTRTextureGouraudAdd.o CTRTextureGouraudNoZ.o CTRTextureGouraudWire.o CZBuffer.o CTRTextureGouraudVertexAlpha2.o CTRTextureGouraudNoZ2.o CTRTextureLightMap2_M2.o CTRTextureLightMap2_M4.o CTRTextureLightMap2_M1.o CSoftwareDriver2.o CSoftwareTexture2.o CTRTextureGouraud2.o CTRGouraud2.o CTRGouraudAlpha2.o CTRGouraudAlphaNoZ2.o CTRTextureDetailMap2.o CTRTextureGouraudAdd2.o CTRTextureGouraudAddNoZ2.o CTRTextureWire2.o CTRTextureLightMap2_Add.o CTRTextureLightMapGouraud2_M4.o IBurningShader.o CTRTextureBlend.o CTRTextureGouraudAlpha.o CTRTextureGouraudAlphaNoZ.o CDepthBuffer.o CBurningShader_Raster_Reference.o IRRIOOBJ = CFileList.o CFileSystem.o CLimitReadFile.o CMemoryFile.o CReadFile.o CWriteFile.o CXMLReader.o CXMLWriter.o CWADReader.o CZipReader.o CPakReader.o CNPKReader.o CTarReader.o CMountPointReader.o irrXML.o CAttributes.o lzma/LzmaDec.o -IRROTHEROBJ = CIrrDeviceSDL.o CIrrDeviceLinux.o CIrrDeviceConsole.o CIrrDeviceStub.o CIrrDeviceWin32.o CIrrDeviceFB.o CLogger.o COSOperator.o Irrlicht.o os.o +IRROTHEROBJ = CIrrDeviceSDL.o CIrrDeviceLinux.o CIrrDeviceConsole.o CIrrDeviceStub.o CIrrDeviceWin32.o CIrrDeviceFB.o CLogger.o COSOperator.o Irrlicht.o os.o leakHunter.o IRRGUIOBJ = CGUIButton.o CGUICheckBox.o CGUIComboBox.o CGUIContextMenu.o CGUIEditBox.o CGUIEnvironment.o CGUIFileOpenDialog.o CGUIFont.o CGUIImage.o CGUIInOutFader.o CGUIListBox.o CGUIMenu.o CGUIMeshViewer.o CGUIMessageBox.o CGUIModalScreen.o CGUIScrollBar.o CGUISpinBox.o CGUISkin.o CGUIStaticText.o CGUITabControl.o CGUITable.o CGUIToolBar.o CGUIWindow.o CGUIColorSelectDialog.o CDefaultGUIElementFactory.o CGUISpriteBank.o CGUIImageList.o CGUITreeView.o ZLIBOBJ = zlib/adler32.o zlib/compress.o zlib/crc32.o zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o zlib/trees.o zlib/uncompr.o zlib/zutil.o JPEGLIBOBJ = jpeglib/jcapimin.o jpeglib/jcapistd.o jpeglib/jccoefct.o jpeglib/jccolor.o jpeglib/jcdctmgr.o jpeglib/jchuff.o jpeglib/jcinit.o jpeglib/jcmainct.o jpeglib/jcmarker.o jpeglib/jcmaster.o jpeglib/jcomapi.o jpeglib/jcparam.o jpeglib/jcprepct.o jpeglib/jcsample.o jpeglib/jctrans.o jpeglib/jdapimin.o jpeglib/jdapistd.o jpeglib/jdatadst.o jpeglib/jdatasrc.o jpeglib/jdcoefct.o jpeglib/jdcolor.o jpeglib/jddctmgr.o jpeglib/jdhuff.o jpeglib/jdinput.o jpeglib/jdmainct.o jpeglib/jdmarker.o jpeglib/jdmaster.o jpeglib/jdmerge.o jpeglib/jdpostct.o jpeglib/jdsample.o jpeglib/jdtrans.o jpeglib/jerror.o jpeglib/jfdctflt.o jpeglib/jfdctfst.o jpeglib/jfdctint.o jpeglib/jidctflt.o jpeglib/jidctfst.o jpeglib/jidctint.o jpeglib/jmemmgr.o jpeglib/jmemnobs.o jpeglib/jquant1.o jpeglib/jquant2.o jpeglib/jutils.o jpeglib/jcarith.o jpeglib/jdarith.o jpeglib/jaricom.o diff --git a/source/Irrlicht/leakHunter.cpp b/source/Irrlicht/leakHunter.cpp new file mode 100644 index 00000000..4935767b --- /dev/null +++ b/source/Irrlicht/leakHunter.cpp @@ -0,0 +1,15 @@ +// Copyright (C) 2013 Michael Zeilfelder +// This file is part of the "Irrlicht Engine". +// For conditions of distribution and use, see copyright notice in irrlicht.h + +#include "leakHunter.h" + +#ifdef _IRR_COMPILE_WITH_LEAK_HUNTER_ + +namespace irr +{ + irr::core::array LeakHunter::ReferenceCountedObjects; +} // end namespace irr + +#endif // _IRR_COMPILE_WITH_LEAK_HUNTER_ +