Is there a way I can safely manage all memory leaks? I mean what should I have in mind before the program closes? What should i release / delete?
Is there a way I can safely manage all memory leaks? I mean what should I have in mind before the program closes? What should i release / delete?
Anything allocated with new is a good place to start. Also make sure you dont allocate space more than once for a pointer, eg in a constructor and in a function. I would also look at 3rd party libraries & functions you are might be using, sometimes there is a deleteObject type function to clean things up when you're done.
There are probably other areas but I'm still thinking...
Free all memory that you explicitly allocated with new, malloc, or realloc.
-Prelude
My best code is written with the delete key.
Ok, thanks. Also, is there a program which can measure the memory available before and after running a program?
void GlobalMemoryStatus(MEMORYSTATUS *);
The members of the MEMORYSTATUS structure are:
DWORD dwLength
DWORD dwTotalPhys
DWORD dwAvailPhys
DWORD dwTotalVirtual
DWORD dwAvailVirtual
DWORD dwMemoryLoad
DWORD dwTotalPageFile
DWORD dwAvailPageFile
Just call it in a loop and examine the contents each iteration.
#include <windows.h>
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Perhaps, but I don't know if that is useful. When you start the program which must count the memory, this program will also take memory. And there are probably other processes taking and freeing memory.
But you can use a tool like Lint to check your code for memory leaks. Such a tool can be found for example here:
http://lclint.cs.virginia.edu/
A C++ code memory leak tracer can be found here:
http://www.andreasen.org/LeakTracer/
And ofcourse be defensive. Do a lot of error-checking, take care of your pointers, take care of writing beyond array ranges etc. Especially when a pointer is passed to a function.
Also take care of more than one malloc in one function:Code:void function (int *ptr) { // If ptr points to an array, take care of its length. }
There are lots of books on safe coding, like:Code:a = malloc (sizeof (int) * 10); if (a == NULL) return FALSE; b = malloc (sizeof (int) * 20); if (b == NULL) { // Remember here that memory for a is already allocated // and should be freed before returning return value free (a); return FALSE; }
Safer C
"Developing for High-Integrity and Safety-Critical Systems"
ISBN 0-07-707640-0
McGraw-Hill, 1995
Les Hatton
Last edited by Shiro; 05-12-2002 at 03:57 AM.
If you got MSVC++ there is a good lib u can use:
[code]
#ifndef NDEBUG
//------------------------------------------------------------------------------
// This code is to turn on and of memleak tracer.
// Will only be active in debug mode
#include <crtdbg.h>
//------------------------------------------------------------------------------
#endif
int main()
{
#ifndef NDEBUG
//------------------------------------------------------------------------------
// This code is to turn on and of memleak tracer.
// Will only be active in debug mode
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag
flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
_CrtSetDbgFlag(flag); // Set flag to the new value
//
//------------------------------------------------------------------------------
#endif
char *pCh = new char[100];
return 0;
}
now you will get info in the debug window telling you there is memory leeks. This way you can always fix the errors the moment they arrise.
What I also want to suggest when fighting memleeks is a coding style where you always write the deallocation code after you have written the allocation code. Then write the code that is between. That way you won't forget you allocated memory in that nested loop 3 pages up. This method might not be as easy to addopt if one function allocates the memory and another deletes it. But that you shouldn't do anyway.
Well I could go on forever... hope this helped...