Jump to content

C++ memory leak detection


AggrorJorn
 Share

Recommended Posts

After a few hours of programming my computer becomes extremely slow. When looking in the task manager I can see my memory usage almost reaching its maximum (8GB!).

 

I have no experience on how to detect memory leaks. Are there any special ways inside visual studio or C++ that allow you to pickup on memory leaks?

 

thanks in advance. smile.png

Link to comment
Share on other sites

ensure that all pointers that are being created and assigned are you have a var for so that when you are done you can delete them.

 

so like

Class *mClass=new Class();

will need to have this when you are finished with it:

delete mClass;

 

OS X instruments tool set has memory usage profiling. it is called something along the lines of malloc debug or something along those lines. you can attach your app to the malloc process right in the xCode editor

bool Life()
{
 while(death=false)
 {
   if(death==true)
   return death;
 }
}

 

I have found the secret to infinite life

 

Did I help you out? Like my post!

Link to comment
Share on other sites

Hi aggror (we spoken last night about it so its just a remembering)

 

It's hard to tell where the problem comes from, here are some simple tips:

 

- Every new requires an delete (as xtreampb has written)

- Every new [] requires an delete[]

Texture* arrayoftextures = new Texture[20]; 
delete[] arrayoftextures; // instead of delete

- Use c++ constructors and destructors

- Use the stack (CConsole myconsole instead of CConsole* myconsole = new CConsole when possible)

- Avoid static/singletons when possible (same as global variables)

 

There are plenty ways to detect a leak, to be honest i haven't done it for some time.

As i remember you can use some CRT functions or overwrite the new/delete operators to create an allocation list.

Link to comment
Share on other sites

The application should clear any memory you leave when it ends. You might have some process running that is leaking? Have you looked at the processes in task manager to see what is consuming the memory?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

This is exactly one of the reason for developing on Linux, rock solid OS where your chance that your app is causing problems are very small and very easy to spot . About windows I am not sure what task manager shows about memory, sometime i know for sure that an app allocates tons of memory but it simply does not show in task manager.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...