Memory cleanup

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

Memory cleanup

Post by KaReL »

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!
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
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: Memory cleanup

Post by Ryan Norton »

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).
Interesting question - I can't really give you a concrete link for it...

(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]
KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

Post by KaReL »

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 ;)
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
Riccardo Cohen

Re: Memory cleanup

Post by Riccardo Cohen »

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]
jamesrf
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Sep 03, 2004 11:26 am
Location: Oxford, UK
Contact:

Re: Memory cleanup

Post by jamesrf »

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.
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.
achim
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 205
Joined: Sun Aug 29, 2004 5:20 pm
Location: Germany

Post by achim »

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! :oops:
So can anybody recommend a mem-leak-detector, suiting to gcc, btw MinGW (using Dev-Cpp)?

tanx
achim
jamesrf
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Sep 03, 2004 11:26 am
Location: Oxford, UK
Contact:

Post by jamesrf »

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! :oops:
So can anybody recommend a mem-leak-detector, suiting to gcc, btw MinGW (using Dev-Cpp)?

tanx
achim
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.

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
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton »

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
    #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
This will tell you the line # etc. of each allocation and which one was leaked even....
[Mostly retired moderator, still check in to clean up some stuff]
prophet
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Sep 01, 2004 6:19 am

Post by prophet »

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).
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton »

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.
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.

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).
prophet wrote: For programs that should run for a longer period of time: cleanup everything offcourse.
Right.
prophet wrote: BTW: I was just thinking, has anyone tried compiling wxWindows with an alternative memory allocator. (dmalloc).
Sure - all it does it replace malloc, for example... you just need to set it up for the appropriate headers, etc...
[Mostly retired moderator, still check in to clean up some stuff]
achim
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 205
Joined: Sun Aug 29, 2004 5:20 pm
Location: Germany

Post by achim »

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.
...
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.
Ryan 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>
   ...
Is this VC-depending :?:

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>
...
Will this declare the _Crt - stuff:
_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
cg
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 201
Joined: Sun Aug 29, 2004 12:33 am
Location: Canada
Contact:

Post by cg »

It probably would be worth starting a topic on different tools that people use...
Profiling, Source Control, Project Mgmt etc, IDE.... I would find it very usefull to hear other peoples experiences.

CG
caseyodonnell
Knows some wx things
Knows some wx things
Posts: 31
Joined: Fri Sep 10, 2004 1:03 pm
Location: Troy, NY
Contact:

Post by caseyodonnell »

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 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...
Post Reply