• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Finding the memory leak

## Recommended Posts

ISOPimp    122
I am getting a memory leak when I exit my program. The program runs as long as I let it, but when I exit the program I get memory leak information in my output screen. I checked for any ''new''s and found 3, I also checked my ''delete''s and found 3 so I am a bit confused. I use safe deletes to delete my memory:
#define SafeDelete(p) if(p){delete (p); (p)=0;}
#define SafeDeleteArray(p) if(p) {delete [] (p); (p)=0;} 
here is the error code Im getting:
  Direct3D9: :====> ENTER: DLLMAIN(00dc34f0): Process Detach 00000640, tid=00000248
Direct3D9: (INFO) :MemFini!
Direct3D9: (ERROR) :Memory still allocated!  Alloc count = 144
Direct3D9: (ERROR) :Current Process (pid) = 00000640
Direct3D9: (ERROR) :Total Memory Unfreed From Current Process = 45992 bytes
Direct3D9: :====> EXIT: DLLMAIN(00dc34f0): Process Detach 00000640  
what is the easiest way to find which memory is leaking? Thanks

##### Share on other sites
PhiberOptic    128
Even though you might have 3 new and 3 delete, you might also have other alloc functions, like different Direct X structures. Are you sure you free them all up? Textures etc.

Also, make sure you call all those delete.. so that you don''t somehow miss to execute them.. try some breakpoints..

##### Share on other sites
ISOPimp    122
Okay, i looked a little harder and found this weird piece of code and I am not sure what it does (I took it from a tutorial):

  	m_pwIndices = new WORD[m_wNumOfIndices];		// build triangles 	WORD* pwIndices = m_pwIndices;	for(dwV = 0; dwV < dwVSize - 1; ++dwV) 	{		for(dwU = 0; dwU < dwUSize - 1; ++dwU) 		{			// get a quad and build two tris out of it			//  v0--v1			//  |   |			//  v2--v3			int v0 = dwV * dwUSize + dwU;			int v1 = v0+1;			int v2 = (dwV + 1) * dwUSize + dwU;			int v3 = v2+1;						pwIndices[0] = v0, pwIndices[1] = v1, pwIndices[2] = v3;			pwIndices[3] = v3, pwIndices[4] = v2, pwIndices[5] = v0;            			pwIndices += 6; //next two tris        		}	}	// compute the normals	ComputeNormals (m_pvVertices, m_pwIndices, 					m_dwNumOfTriangles, m_dwNumOfVertices);}

What is confusing for me is the fact that pwIndices gets all minipulated and such but is never used. furthermore, once I do my first operation on pwIndicies, in my watch window, the contents of m_pwIndices turns to 0. Do I need to delete the pwIndices memory? Does this code look valid? (some code was omitted because it was irrelivent.)

##### Share on other sites
Dobbs    164

By the way your safe delete macros are kind of worthless. It's standard behaviour for delete and delete [] to do nothing when given a zero pointer.

[edited by - Dobbs on May 28, 2003 4:42:18 PM]

##### Share on other sites
fizban75    130
No, you don''t need to delete pwIndices. It''s just a temporary pointer that starts out pointing to the same memory location as the beginning of m_pwIndices, then traverses along the rest of the array. You might want to look at a tutorial on pointers.

You''re sure you''re using delete [] in all the right places?

##### Share on other sites
ISOPimp    122
are you sure pwIndices is being used by ComputeNormals? I only see m_pwIndices being passed (They are different right?).

Anyway, I can watch all I want but I dont really get it so here it is. I will post the suspicious code again with watch information in comments after the line that executes:

    	m_pwIndices = new WORD[m_wNumOfIndices];//m_pwIndices = Location: 0x00d1b2b8  Value: 52685//pwIndices   = Location: 0xcccccccc  Value: <Bad Ptr>		// build triangles 	WORD* pwIndices = m_pwIndices;//m_pwIndices = Location: 0x00d1b2b8  Value: 52685//pwIndices   = Location: 0x00d1b2b8  Value: 52685	for(dwV = 0; dwV < dwVSize - 1; ++dwV) 	{		for(dwU = 0; dwU < dwUSize - 1; ++dwU) 		{			// get a quad and build two tris out of it			//  v0--v1			//  |   |			//  v2--v3			int v0 = dwV * dwUSize + dwU;			int v1 = v0+1;			int v2 = (dwV + 1) * dwUSize + dwU;			int v3 = v2+1;						pwIndices[0] = v0, pwIndices[1] = v1, pwIndices[2] = v3;			pwIndices[3] = v3, pwIndices[4] = v2, pwIndices[5] = v0;            			pwIndices += 6; //next two tris        		}	}//m_pwIndices = Location: 0x00d1b2b8  Value: 0//pwIndices   = Location: 0x00d1e2b8  Value: 65021	// compute the normals	ComputeNormals (m_pvVertices, m_pwIndices, 					m_dwNumOfTriangles, m_dwNumOfVertices);//m_pwIndices = Location: 0x00d1b2b8  Value: 0//pwIndices   = Location: 0x00d1e2b8  Value: 65021}

Then the function ends and I get no memory leak information at this point, pwIndices goes out of scope, and m_pwIndices remains what it was last (Location: 0x00d1b2b8 Value: 0).
Anyway, keep that help coming

fizban75: I am using delete [] at the moment because the variables are declared like new WORD[num]; but out of frustration I have tried using just the regular delete and I get the same result.

Thanks

[edited by - ISOPimp on May 28, 2003 4:48:06 PM]