How to identify memory leaks? (file/line)

EmoryM
Posts: 4
Joined: Sun Oct 02, 2011 10:07 am

How to identify memory leaks? (file/line)

Post by EmoryM »

In my engine I'm using a define and new( _NORMAL_BLOCK, __FILE__, __LINE__ ) to detect memory leaks. How would I accomplish something similar in Bullet?

I looked at building the libraries with BT_DEBUG_MEMORY_ALLOCATIONS defined but that was more than I bargained for. I looked at the thread regarding btAlignedAllocSetCustom, but I don't think that can be used for my purposes either.

Thanks!
CookieMonster
Posts: 49
Joined: Sun Jan 29, 2012 10:01 pm

Re: How to identify memory leaks? (file/line)

Post by CookieMonster »

A good way to avoid memory leaks is to have a high level macro in each class that put most constructors and destructors on the same lines.
The twisted code in this example is from my graphics engine:

Code: Select all

// Data collection macros
	// Variables
		#define CORE_DATA_VAR_DECLARE(DATATYPE,IDENTIFIER,DEFAULTVALUE) DATATYPE IDENTIFIER;
		#define CORE_DATA_VAR_INIT(DATATYPE,IDENTIFIER,DEFAULTVALUE) IDENTIFIER = DEFAULTVALUE;
		#define CORE_DATA_VAR_ALLOCATE(DATATYPE,IDENTIFIER,DEFAULTVALUE) ;
		#define CORE_DATA_VAR_RELEASE(DATATYPE,IDENTIFIER,DEFAULTVALUE) ;
	// Fixed size arrays
		#define CORE_DATA_ARRAY_DECLARE(DATATYPE,IDENTIFIER,ARRAY_SIZE) DATATYPE IDENTIFIER[ARRAY_SIZE];
		#define CORE_DATA_ARRAY_INIT(DATATYPE,IDENTIFIER,ARRAY_SIZE) memset(&(IDENTIFIER),0,sizeof(DATATYPE) * ARRAY_SIZE);
		#define CORE_DATA_ARRAY_ALLOCATE(DATATYPE,IDENTIFIER,ARRAY_SIZE) ;
		#define CORE_DATA_ARRAY_RELEASE(DATATYPE,IDENTIFIER,ARRAY_SIZE) ;
	// Pointers
		#define CORE_DATA_PTR_DECLARE(DATATYPE,IDENTIFIER,ALLOCATE,RELEASE) DATATYPE IDENTIFIER;
		#define CORE_DATA_PTR_INIT(DATATYPE,IDENTIFIER,ALLOCATE,RELEASE) IDENTIFIER = NULL;
		#define CORE_DATA_PTR_ALLOCATE(DATATYPE,IDENTIFIER,ALLOCATE,RELEASE) ALLOCATE;
		#define CORE_DATA_PTR_RELEASE(DATATYPE,IDENTIFIER,ALLOCATE,RELEASE) Try_Start { RELEASE; } Try_Else { \
		  ::MessageBoxW(NULL,L#IDENTIFIER,L"Releasing the following pointer caused access violation.",NULL); }
	// Anything else
		#define CORE_DATA_ANY_DECLARE(NAME,DECLARATION,INIT,ALLOCATE,RELEASE) DECLARATION;
		#define CORE_DATA_ANY_INIT(NAME,DECLARATION,INIT,ALLOCATE,RELEASE) INIT;
		#define CORE_DATA_ANY_ALLOCATE(NAME,DECLARATION,INIT,ALLOCATE,RELEASE) ALLOCATE;
		#define CORE_DATA_ANY_RELEASE(NAME,DECLARATION,INIT,ALLOCATE,RELEASE) Try_Start { RELEASE; } Try_Else { \
		  ::MessageBoxW(NULL,L#NAME,L"Releasing the following data caused access violation.",NULL); }

// The table where you declare everything in one place
#define CORE_DATA_TABLE(SUBMACRO) \
	CORE_DATA_VAR_##SUBMACRO(int,m_SeenInstances,0) \
	CORE_DATA_ANY_##SUBMACRO(m_PostEffectVS,VertexShader m_PostEffectVS,DEFAULT_SHADER_INIT(m_PostEffectVS),m_PostEffectVS = \
	  Shader_CreateVSFromString(CommonPostEffectVertexShader,ShaderType_PostEffect),DEFAULT_SHADER_RELEASE(m_PostEffectVS)) \
	CORE_DATA_ARRAY_##SUBMACRO(int,m_PostEffect_InputTextures,16) \
	CORE_DATA_PTR_##SUBMACRO(Shader_Struct*,m_pDefaultMaterialShader,;,;)
Put these macros where you would declare, initiate and release things in your class:

Code: Select all

CORE_DATA_TABLE(DECLARE)
CORE_DATA_TABLE(INIT)
CORE_DATA_TABLE(RELEASE)
You can also try "CppCheck" to find runtime errors without running your application.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: How to identify memory leaks? (file/line)

Post by Erwin Coumans »

I looked at building the libraries with BT_DEBUG_MEMORY_ALLOCATIONS defined but that was more than I bargained for.
Do you just need to know IF there are memory leaks, or do you need to find WHERE the memoryleaks are?

If you enable #define BT_DEBUG_MEMORY_ALLOCATIONS in bullet2\LinearMath\btAlignedAllocator.h
gTotalBytesAlignedAllocs has the current outstanding bytes allocated. At the end of this program, it should be zero. This helps with the IF part.

If you want to know where this memory leaks happened under Visual Studio, I can recommend using the free open source Visual Leak Detector (http://vld.codeplex.com/releases)
and link against the Multi-threaded Debug DLL (/MDd). Here is some example output:

Code: Select all

WARNING: Visual Leak Detector detected memory leaks!
---------- Block 23 at 0x02485FD8: 727 bytes ----------
  Call Stack:
    d:\develop\2012\private_experiments\bullet2\linearmath\btalignedallocator.cpp (132): OpenCL_gpu_rigidbody_pipeline2_AMD_debug.exe!btAlignedAllocInternal + 0x13 bytes
    d:\develop\2012\private_experiments\bullet2\bulletcollision\collisiondispatch\btcollisionobject.h (115): OpenCL_gpu_rigidbody_pipeline2_AMD_debug.exe!btCollisionObject::operator new + 0x15 bytes
    d:\develop\2012\private_experiments\opencl\gpu_rigidbody_pipeline2\main.cpp (64): OpenCL_gpu_rigidbody_pipeline2_AMD_debug.exe!main + 0xA bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (555): OpenCL_gpu_rigidbody_pipeline2_AMD_debug.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): OpenCL_gpu_rigidbody_pipeline2_AMD_debug.exe!mainCRTStartup
    0x76FF339A (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77C09EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
    0x77C09EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Alternatively, you can use attached replacement for LinearMath/btAlignedAllocator.cpp, just enable the #define BT_DEBUG_MEMORY_ALLOCATIONS in LinearMath/btAlignedAllocator.h
If you then use _CrtDumpMemoryLeaks(); it will show

Code: Select all

Detected memory leaks!
Dumping objects ->
d:\develop\2012\private_experiments\bullet2\bulletcollision\collisiondispatch\btCollisionObject.h(115) : {123} normal block at 0x02255FD8, 727 bytes long.
 Data: <     _%   /     > C0 02 00 00 D8 5F 25 02 A0 04 2F 00 CD CD CD CD 
Object dump complete.
Hope this helps,
Erwin
You do not have the required permissions to view the files attached to this post.
EmoryM
Posts: 4
Joined: Sun Oct 02, 2011 10:07 am

Re: How to identify memory leaks? (file/line)

Post by EmoryM »

Thanks! I went ahead and followed your advice regarding using Visual Leak Detector - it is indeed an awesome tool.

The leak had nothing to do with Bullet :lol: