memory leak
-
hi, how do i solve the "memory leak" problem in my VC++ project? is it calling SetProcessWorkingSetSize function?
Zo.Naderi-Iran
-
hi, how do i solve the "memory leak" problem in my VC++ project? is it calling SetProcessWorkingSetSize function?
Zo.Naderi-Iran
1. Search for all occurrences of the
new
keyword. 2. Make sure they have a correspondingdelete
(ordelete []
where applicable) 3. Search foralloc
(will catch malloc, calloc, ...) 4. Make sure they have a correspondingfree()
. What else? Do you use COM components? GDI? Files? Databases? Registry? Try to run a subset of your application with a low code coverage to see if you still leak to narrow it down. (This might require code changes.) Increase the coverage in steps. A code review can also make wonders. Purchase a detection tool if your budget allows it. -
hi, how do i solve the "memory leak" problem in my VC++ project? is it calling SetProcessWorkingSetSize function?
Zo.Naderi-Iran
The best way to avoid memory leaks is not to cause them in the first place. So.... - avoid using new and delete, make your objects automatic and use "parameterise from the top" - if you're using arrays use std::vector instead - if you're using arrays of characters use std::string instead - use classes to manage resources generally - google RAII and have a read - don't use raw pointers unless you're interacting with legacy code Cheers, Ash PS: For the pedants - you can break all these rules, but have a reason to apart from "I didn't do that in C" or "I read it in a book by Herb Schildt"
-
1. Search for all occurrences of the
new
keyword. 2. Make sure they have a correspondingdelete
(ordelete []
where applicable) 3. Search foralloc
(will catch malloc, calloc, ...) 4. Make sure they have a correspondingfree()
. What else? Do you use COM components? GDI? Files? Databases? Registry? Try to run a subset of your application with a low code coverage to see if you still leak to narrow it down. (This might require code changes.) Increase the coverage in steps. A code review can also make wonders. Purchase a detection tool if your budget allows it. -
The best way to avoid memory leaks is not to cause them in the first place. So.... - avoid using new and delete, make your objects automatic and use "parameterise from the top" - if you're using arrays use std::vector instead - if you're using arrays of characters use std::string instead - use classes to manage resources generally - google RAII and have a read - don't use raw pointers unless you're interacting with legacy code Cheers, Ash PS: For the pedants - you can break all these rules, but have a reason to apart from "I didn't do that in C" or "I read it in a book by Herb Schildt"
-
yes, i checked my project. all new keyword have delete and ... an infinite loop there is in my project. in this loop, i new and delete several pointer. is it useful , the calling SetProcessWorkingSetSize , in first or end of loop?
Zo.Naderi-Iran
You have a serious problem in your code, and
SetProcessWorkingSetSize
will unfortunatly not save you. Try and remove the new/delete in your loop if possible. Are you sure you don't have any condtional deletes? (whithin if/else block?) Does all branches clean up as expected? I didn't ask, but how is the leak showing? Can you spot it from the task manager while executing, or does visual studio inform you when you exit? -
Mr. Schildt has written loads of rather bad books about C++. I've met plenty of poor programmers that have learnt C from one of his books thinking they were learning C++. And more annoyingly I bought a couple of his books incredibly cheap from a remaindered book store in 1998 and even though each was the price of a pint of beer at the time I felt exceedingly short changed after reading them. He's not unique BTW, there are plenty of appalling books out there that try to teach C++ but he's churned out far more than most of the others. What I find particularly distrurbing are the number of professional educators that don't seem to be able to tell what's a good teaching text and what isn't. Cheers, Ash PS: Just so I'm not being wholly negative, there are two very good books to learn C++ from: - "Accelerated C++" by Koenig and Moo - this is great for experienced programmers who want to learn C++ - "Programming -- Principles and Practice Using C++" by Stroustrup. This is better for people who have no exposure to programming but still gives them a good grounding.
modified on Wednesday, June 9, 2010 7:34 AM
-
You have a serious problem in your code, and
SetProcessWorkingSetSize
will unfortunatly not save you. Try and remove the new/delete in your loop if possible. Are you sure you don't have any condtional deletes? (whithin if/else block?) Does all branches clean up as expected? I didn't ask, but how is the leak showing? Can you spot it from the task manager while executing, or does visual studio inform you when you exit? -
Ok, so let's assume the leak really is from within the loop. Do you maybe have any exception / exception handling where the new/delete pair breaks? like:
try {
int *p = new int;
if (bNowAndThen)
throw "Exception";
delete p;
}
catch (const char* e)
{
}If not, you need to start inspecting the functions you call from the loop and see if any of them is causing this. Do this by removing as much as you can, recompile and run. If it looks ok try adding a few calls until the error occurs again.
-
Ok, so let's assume the leak really is from within the loop. Do you maybe have any exception / exception handling where the new/delete pair breaks? like:
try {
int *p = new int;
if (bNowAndThen)
throw "Exception";
delete p;
}
catch (const char* e)
{
}If not, you need to start inspecting the functions you call from the loop and see if any of them is causing this. Do this by removing as much as you can, recompile and run. If it looks ok try adding a few calls until the error occurs again.
-
The best way to avoid memory leaks is not to cause them in the first place. So.... - avoid using new and delete, make your objects automatic and use "parameterise from the top" - if you're using arrays use std::vector instead - if you're using arrays of characters use std::string instead - use classes to manage resources generally - google RAII and have a read - don't use raw pointers unless you're interacting with legacy code Cheers, Ash PS: For the pedants - you can break all these rules, but have a reason to apart from "I didn't do that in C" or "I read it in a book by Herb Schildt"