Memory cleanup
Memory cleanup
This is not a general wxWidgets question...
Actually, it is about this bug: https://sourceforge.net/tracker/?func=d ... up_id=9863
Now my question:
When I have the following program:
[syntax="c"]int main( void )
{
int * i = new int;
return 0;
}[/syntax]
it leaks sizeof(int) bytes... Now can someone tell me where I can find some real proof that this will be deleted by windows? (this int).
Because I find it hard to believe that programmers are always trying to hunt down memory leaks when it all gets cleaned up by the operating system.
(PS: this is a windows-specific question, Is it possible to provide me with some credible webpage or explanation or msdn-page or ... so I can defend my point or agree with the other person in this ongoing discussion).
Thanks!
Actually, it is about this bug: https://sourceforge.net/tracker/?func=d ... up_id=9863
Now my question:
When I have the following program:
[syntax="c"]int main( void )
{
int * i = new int;
return 0;
}[/syntax]
it leaks sizeof(int) bytes... Now can someone tell me where I can find some real proof that this will be deleted by windows? (this int).
Because I find it hard to believe that programmers are always trying to hunt down memory leaks when it all gets cleaned up by the operating system.
(PS: this is a windows-specific question, Is it possible to provide me with some credible webpage or explanation or msdn-page or ... so I can defend my point or agree with the other person in this ongoing discussion).
Thanks!
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Re: Memory cleanup
Interesting question - I can't really give you a concrete link for it...KaReL wrote: (PS: this is a windows-specific question, Is it possible to provide me with some credible webpage or explanation or msdn-page or ... so I can defend my point or agree with the other person in this ongoing discussion).
(The following is from my memory banks )
However, windows didn't always destroy memory leaks like it does now - it didn't until windows 95 came along and gave each program it's own address space - before then memory leaks were huge deals because they weren't freed on the program's exit.
Anyway, IIRC since win95 it has a better heap-based allocater that takes care of the memory leaks.
The above is just a wild guess - I don't actually recall any book ever laying this out at all (I didn't even know until someone on a mailing list told me).
[Mostly retired moderator, still check in to clean up some stuff]
For your interest: I asked it on another (Dutch) forum:
New pointers are created on the heap, this is thrown away on program exit. Of course this throw away doesn't handle destructors, it is merely a cleanup. Therefor it is a good practice to clean up things yourself.
But in this specific bugcase, it doesn't really matter.
Thanks
New pointers are created on the heap, this is thrown away on program exit. Of course this throw away doesn't handle destructors, it is merely a cleanup. Therefor it is a good practice to clean up things yourself.
But in this specific bugcase, it doesn't really matter.
Thanks
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be
Re: Memory cleanup
This is my opinion, having worked on real project with Java, Eiffel, C/C++, and Basic.
All depends on what you're doing. Lets assume that all operating system cleans up everything at program end.
- Then if you make a little tool that is launched once or twice a day, memory leaks are not a problem.
- If you make an all day software, like mail client, office-like spreadsheet..., then you'd better free all memory, because some data may be huge, or some data may be frequenlty allocated. Then it is not a good idea to let all that memory lost. your program will take thousands of megas of memory at the end of the day
- If you make a server (like apache...) then you MUST free everything, because your system may crash rapidly, and your software would usable
I always free everything, and check for memory leaks in C, because this is a small part of my personnal rules for making good software.
[quote="KaReL"]This is not a general wxWidgets question...
Actually, it is about this bug: [url]https://sourceforge.net/tracker/?func=d ... up_id=9863[/url]
Now my question:
When I have the following program:
[syntax="c"]int main( void )
{
int * i = new int;
return 0;
}[/syntax]
it leaks sizeof(int) bytes... Now can someone tell me where I can find some real proof that this will be deleted by windows? (this int).
Because I find it hard to believe that programmers are always trying to hunt down memory leaks when it all gets cleaned up by the operating system.
(PS: this is a windows-specific question, Is it possible to provide me with some credible webpage or explanation or msdn-page or ... so I can defend my point or agree with the other person in this ongoing discussion).
Thanks![/quote]
All depends on what you're doing. Lets assume that all operating system cleans up everything at program end.
- Then if you make a little tool that is launched once or twice a day, memory leaks are not a problem.
- If you make an all day software, like mail client, office-like spreadsheet..., then you'd better free all memory, because some data may be huge, or some data may be frequenlty allocated. Then it is not a good idea to let all that memory lost. your program will take thousands of megas of memory at the end of the day
- If you make a server (like apache...) then you MUST free everything, because your system may crash rapidly, and your software would usable
I always free everything, and check for memory leaks in C, because this is a small part of my personnal rules for making good software.
[quote="KaReL"]This is not a general wxWidgets question...
Actually, it is about this bug: [url]https://sourceforge.net/tracker/?func=d ... up_id=9863[/url]
Now my question:
When I have the following program:
[syntax="c"]int main( void )
{
int * i = new int;
return 0;
}[/syntax]
it leaks sizeof(int) bytes... Now can someone tell me where I can find some real proof that this will be deleted by windows? (this int).
Because I find it hard to believe that programmers are always trying to hunt down memory leaks when it all gets cleaned up by the operating system.
(PS: this is a windows-specific question, Is it possible to provide me with some credible webpage or explanation or msdn-page or ... so I can defend my point or agree with the other person in this ongoing discussion).
Thanks![/quote]
-
- Earned a small fee
- Posts: 14
- Joined: Fri Sep 03, 2004 11:26 am
- Location: Oxford, UK
- Contact:
Re: Memory cleanup
Absolutely! In addition, I'd say that 1 time in 3 (or more), the fix is not just a case of a missing delete. It often shows up very real bugs in the software. I've lost count of the amount of times this has happened. It really bugs me when I use a library with memory leaks. I am constantly amazed at how sloppy some people are where memory leaks are concered.Riccardo Cohen wrote: I always free everything, and check for memory leaks in C, because this is a small part of my personnal rules for making good software.
-
- Earned a small fee
- Posts: 14
- Joined: Fri Sep 03, 2004 11:26 am
- Location: Oxford, UK
- Contact:
I don't use any 3rd party leak detector. Instead I just look at the output window in Visual Studio. It just uses the _Crt suite of functions. There are probably equivalent funcs in your environment. Just doing this keeps all our code 100% leak free (and as I said earlier it really helps in other ways too). Also, for this to work you need to steer clear of allocating from the heap from static objects.achim wrote:hi,
very interesting theme, I've to admit, that my mem-leak checking is
limited to detect the W2K taskmanager while debugging, little less I guess!
So can anybody recommend a mem-leak-detector, suiting to gcc, btw MinGW (using Dev-Cpp)?
tanx
achim
If you interested I wrote this little class which works on windows and can be used in unit tests:
/**
* \class MemoryLeakDetector.
* \brief Utility class for detecting memory leaks.
*
* This class can be used to check if there are memory
* leaks between two points in code. When it is instantiated
* a checkpoint is taken. At any subsequent point checkForLeaks()
* can be called. The return code can be checked to see
* if there were any leaks. Another checkpoint is automatically
* set when checkForLeaks() is called. Checkpoints can also
* be set manually using checkpoint(). Only works when _DEBUG is defined.
*/
class MemoryLeakDetector
{
public:
MemoryLeakDetector(bool dumpResults=true, bool inludeCRuntime=true);
~MemoryLeakDetector();
void checkpoint();
bool checkForLeaks();
private:
_CrtMemState m_snapshot;
bool m_dumpResults;
bool m_includeCRuntime;
int m_previousDbgFlag;
};
MemoryLeakDetector::MemoryLeakDetector(bool dumpResults, bool includeCRuntime)
: m_dumpResults(dumpResults),
m_includeCRuntime(includeCRuntime)
{
#ifdef _DEBUG
if (m_includeCRuntime)
{
m_previousDbgFlag = _crtDbgFlag;
_CrtSetDbgFlag(_crtDbgFlag | _CRTDBG_CHECK_CRT_DF );
}
checkpoint();
#endif
}
MemoryLeakDetector::~MemoryLeakDetector()
{
#ifdef _DEBUG
if (m_includeCRuntime)
_CrtSetDbgFlag(m_previousDbgFlag);
#endif
}
/**
* Set a memory leak checkpoint.
*/
void MemoryLeakDetector::checkpoint()
{
#ifdef _DEBUG
_CrtMemCheckpoint(&m_snapshot);
#endif
}
/**
* Check if there have been any memory leaks since the last checkpoint.
* \n Returns <tt>true</tt> if there have been leaks and <tt>false</tt> if not.
*/
bool MemoryLeakDetector::checkForLeaks()
{
#ifdef _DEBUG
_CrtMemState snapshot2 = {0};
_CrtMemCheckpoint(&snapshot2);
_CrtMemState snapshotDiff;
bool leakDetected = _CrtMemDifference(&snapshotDiff, &m_snapshot, &snapshot2) ? true : false;
if (leakDetected && m_dumpResults)
_CrtMemDumpAllObjectsSince(&m_snapshot);
checkpoint();
return leakDetected;
#else
return false;
#endif
}
Hope this helps,
James
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
As long as you use new (which you absolutely should if you're using C++), the following is what wxWidgets uses for memory detection -
This will tell you the line # etc. of each allocation and which one was leaked even....
Code: Select all
// VC++ uses this macro as debug/release mode indicator
#ifdef _DEBUG
// Need to undef new if including crtdbg.h which redefines new itself
#ifdef new
#undef new
#endif
// we need this to show file & line number of the allocation that caused
// the leak
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#ifndef _CRTBLD
// Need when builded with pure MS SDK
#define _CRTBLD
#endif
#include <crtdbg.h>
// this define works around a bug with inline declarations of new, see
//
// http://support.microsoft.com/support/kb/articles/Q140/8/58.asp
//
// for the details
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
[Mostly retired moderator, still check in to clean up some stuff]
It's an optimization to not free large pieces of memory on shutdown. When you need, for example, 250 mb of memory for some difficult calculation and you're program will shutdown when the calculation is done. It will be faster to let the OS clean it up. Why ?
Because if the memory is swapped out, and you have alot of small pieces of memory, free-ing every piece would mean swapping in every piece, and then freeing it.
You would probably be better off letting the OS handle it.
For programs that should run for a longer period of time: cleanup everything offcourse.
BTW: I was just thinking, has anyone tried compiling wxWindows with an alternative memory allocator. (dmalloc).
Because if the memory is swapped out, and you have alot of small pieces of memory, free-ing every piece would mean swapping in every piece, and then freeing it.
You would probably be better off letting the OS handle it.
For programs that should run for a longer period of time: cleanup everything offcourse.
BTW: I was just thinking, has anyone tried compiling wxWindows with an alternative memory allocator. (dmalloc).
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Yes and no. If you are on windows or another OS that allows direct access to the heap or a custom allocator (dmalloc, std::allocator, etc.) - then you should do it in your application.prophet wrote:It's an optimization to not free large pieces of memory on shutdown. When you need, for example, 250 mb of memory for some difficult calculation and you're program will shutdown when the calculation is done. It will be faster to let the OS clean it up. Why ?
Because if the memory is swapped out, and you have alot of small pieces of memory, free-ing every piece would mean swapping in every piece, and then freeing it.
You would probably be better off letting the OS handle it.
I.E. on windows you'd call DestroyHeap and it would just destroy the whole heap in one pass, which is what windows does implicitly if you're not using the c runtime (which is slow allocating memory compared to windows heap routines anyway).
Right.prophet wrote: For programs that should run for a longer period of time: cleanup everything offcourse.
Sure - all it does it replace malloc, for example... you just need to set it up for the appropriate headers, etc...prophet wrote: BTW: I was just thinking, has anyone tried compiling wxWindows with an alternative memory allocator. (dmalloc).
[Mostly retired moderator, still check in to clean up some stuff]
I'm new and unexperianced to the memLeak detection, and got no idea, how to get the "equivalent funcs in" Dev-Cpp, if there are any at all.jamesrf wrote: I don't use any 3rd party leak detector. Instead I just look at the output window in Visual Studio. It just uses the _Crt suite of functions. There are probably equivalent funcs in your environment.
...
Is this VC-dependingRyan Norton wrote: As long as you use new (which you absolutely should if you're using C++), the following is what wxWidgets uses for memory detection -Code: Select all
// VC++ uses this macro as debug/release mode indicator #ifdef _DEBUG // Need to undef new if including crtdbg.h which redefines new itself ... #include <crtdbg.h> ...
Code: Select all
...
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#ifndef _CRTBLD
// Need when builded with pure MS SDK
#define _CRTBLD
#endif
#include <crtdbg.h>
...
_CrtMemState
_crtDbgFlag
_CRTDBG_CHECK_CRT_DF
_CrtSetDbgFlag
_CrtMemCheckpoint
_CrtMemDifference
Including crtdbg.h will lead to the following warnings and errors:
..include/crtdbg.h:558:8: warning: extra tokens at end of #endif directive
..error: syntax error before `->' token
In file included from F:/wxWindows-2.5.2/wxWidgets/include/wx/string.h:1289,
from F:/wxWindows-2.5.2/wxWidgets/include/wx/memory.h:20,
from F:/wxWindows-2.5.2/wxWidgets/include/wx/object.h:25,
from F:/wxWindows-2.5.2/wxWidgets/include/wx/wx.h:16,
from libase.cpp:12:
<internal>: In member function `wxString* wxCArrayString::GetStrings()':
<internal>:342: error: too many arguments to function
`void* operator new [](unsigned int)'
F:/wxWindows-2.5.2/wxWidgets/include/wx/arrstr.h:342: error: at this point in file
In file included from F:/wxWindows-2.5.2/wxWidgets/include/wx/wx.h:23,
from libase.cpp:12:
<internal>: In copy constructor `wxDropFilesEvent::wxDropFilesEvent(const
wxDropFilesEvent&)':
<internal>:1489: error: too many arguments to function
`void* operator new [](unsigned int)'
...
So I guess, crtdbg.h will work with VC, but with MinGW (gcc) ?!?
Maybe this qustions are foolish, but I need an entrance to this stuff
achim
-
- Knows some wx things
- Posts: 31
- Joined: Fri Sep 10, 2004 1:03 pm
- Location: Troy, NY
- Contact:
I think I would end up deleting it myself out of habbit. I just can't bring myself to leak memory no matter the situation. It's just something you're supposed to do. It's the programming equivalent of not flushing...prophet wrote:It's an optimization to not free large pieces of memory on shutdown. When you need, for example, 250 mb of memory for some difficult calculation and you're program will shutdown when the calculation is done. It will be faster to let the OS clean it up. Why ?
Because if the memory is swapped out, and you have alot of small pieces of memory, free-ing every piece would mean swapping in every piece, and then freeing it.
You would probably be better off letting the OS handle it.