6 Replies - 3295 Views - Last Post: 07 December 2006 - 11:13 PM Rate Topic: -----

#1 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Find memory leaks

Posted 07 December 2006 - 08:46 AM

I have been playing with stuff like linked list and i think i clean up all of my memory but i just wanted to know if there was any easy way of finding memory leaks that might happen

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Find memory leaks

#2 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: Find memory leaks

Posted 07 December 2006 - 09:12 AM

Use a debugger, add watches and check the values have been deleted.
Was This Post Helpful? 0
  • +
  • -

#3 gordo  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-December 06

Re: Find memory leaks

Posted 07 December 2006 - 09:11 PM

I had the same problem with a very big project.
So I added two functions...GetMemory() and FreeMemory().
Within those I had my standard alloc and free functions but I also made a global variable TotalMemoryCount.

For each GetMemory I'd increment the count, and for each FreeMemory I'd decrement it. If at the end of my program the TotalMemoryCount was not zero, there was a problem.

I also keep another variable for actual memory used that's updated in each of those calls as well.

I have since always used these two functions.
Was This Post Helpful? 0
  • +
  • -

#4 eXceed69  Icon User is offline

  • "Super Sentai Knight Of DawN"
  • member icon

Reputation: 7
  • View blog
  • Posts: 688
  • Joined: 12-November 06

Re: Find memory leaks

Posted 07 December 2006 - 09:13 PM

How bout new and delete keywords rather than those things....
Was This Post Helpful? 0
  • +
  • -

#5 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Find memory leaks

Posted 07 December 2006 - 09:19 PM

Try this Visual Leak Detector - Enhanced Memory Leak Detection for Visual C++
Was This Post Helpful? 0
  • +
  • -

#6 gordo  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-December 06

Re: Find memory leaks

Posted 07 December 2006 - 09:34 PM

View PosteXceed69, on 7 Dec, 2006 - 09:13 PM, said:

How bout new and delete keywords rather than those things....


That won't help if you forget to delete or alloc one.

How could that happen?

In a project with 30,000 lines of code, I've done it. More so when I was learning.

As a note, this is the actual code called when you call 'new'

void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
		{	   // try to allocate size bytes
		void *p;
		while ((p = malloc(size)) == 0)
				if (_callnewh(size) == 0)
				{	   // report no memory
				static const std::bad_alloc nomem;
				_RAISE(nomem);
				}

		return (p);
		}


This post has been edited by gordo: 07 December 2006 - 09:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Find memory leaks

Posted 07 December 2006 - 11:13 PM

Thanks for the replys... i did Xing's way... every easy and it works good
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1