Memory Leak Noob Question

Ok let's say I have this code:

int main()
{
MainApp *app = new MainApp();
app->StartApplication();
return 0;
}

Even though I don't delete the "app" pointer, once the program terminates all memory is freed right?
[299 byte] By [engineer2004] at [2007-11-19 1:20:29]
# 1 Re: Memory Leak Noob Question
Even though I don't delete the "app" pointer, once the program terminates all memory is freed right?
Well...this depends on the operating system...thus, you should not rely on it and therefore free any dynamically allocated memory accordingly...
Andreas Masur at 2007-11-9 0:36:55 >
# 2 Re: Memory Leak Noob Question
Well...this depends on the operating system...thus, you should not rely on it and therefore free any dynamically allocated memory accordingly...

How come not all operating systems have this safeguard?
engineer2004 at 2007-11-9 0:38:03 >
# 3 Re: Memory Leak Noob Question
One could say: the more safeguards the OS contains, the slower and heavier it will be.

For embedded software for example, it might be better to have as light an OS as possible. Then, you have to make sure that all applications running on it are well designed ...
Elrond at 2007-11-9 0:39:01 >
# 4 Re: Memory Leak Noob Question
Unless it is required to retain the state of the embedded system, when it comes to shutting down the system, we just need to unplug the power supplies. :cool:
Kheun at 2007-11-9 0:39:56 >
# 5 Re: Memory Leak Noob Question
Why not just program it as

int main()
{
MainApp app;
app.StartApplication();
return 0;
}

app is created on the stack and destroyed when it goes out of scope.
cup at 2007-11-9 0:41:01 >
# 6 Re: Memory Leak Noob Question
If your MainApp object is large enough that you must create it on the heap, try wrapping the MainApp pointer with a smart pointer, like boost::scoped_ptr. When the program exits, the scoped_ptr destructor will be called, which will free the memory used by the MainApp object.
Bob Davis at 2007-11-9 0:42:00 >
# 7 Re: Memory Leak Noob Question
Ouch, that's scary. I've been told by other programmers (not on CG) that the application resources are returned to the system once the program terminates. (Regardless of whether or not memory has been allocated on the heap)
engineer2004 at 2007-11-9 0:43:04 >
# 8 Re: Memory Leak Noob Question
Unless it is required to retain the state of the embedded system, when it comes to shutting down the system, we just need to unplug the power supplies. :cool:

I can see how embedded systems do not have this type of safeguard.
I'm talking more about PC Windows development.
engineer2004 at 2007-11-9 0:44:04 >
# 9 Re: Memory Leak Noob Question
Well, ... it's true on some OSes, but it is bad programming on any OS to do that !

What is scary is that some programmer actually told you that it is all right to do that because the OS will take care of it !
Elrond at 2007-11-9 0:45:01 >
# 10 Re: Memory Leak Noob Question
I can see how embedded systems do not have this type of safeguard.
I'm talking more about PC Windows development.I believe that Windows 3.x or MSDOS did not release memory on application termination.

Thinking back, I'm sure that Windows 3.x did not release resources on application termination. That's why invariably, you had to restart Windows (or reboot the PC) if you had an application that misbehaved in this manner.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 0:46:07 >
# 11 Re: Memory Leak Noob Question
Well, ... it's true on some OSes, but it is bad programming on any OS to do that !

What is scary is that some programmer actually told you that it is all right to do that because the OS will take care of it !

Yeah I was told that ... but for some reason I had a funny feeling. It's glad I came here to confirm. :)
engineer2004 at 2007-11-9 0:47:06 >
# 12 Re: Memory Leak Noob Question
If your MainApp object is large enough that you must create it on the heap, try wrapping the MainApp pointer with a smart pointer, like boost::scoped_ptr. When the program exits, the scoped_ptr destructor will be called, which will free the memory used by the MainApp object.

Why not std::auto_ptr instead (since it comes with c++)? I'm asking because I haven't used smartpointers yet but am looking into it, and I never see std::auto_ptr mentioned. Are there problems with it?
Martin O at 2007-11-9 0:48:10 >
# 13 Re: Memory Leak Noob Question
There are certain limitations with std::auto_ptr. None of those are really relevant to this example, so auto_ptr would work fine. However, a word of caution: never use an auto_ptr in an STL container. Bad things will happen!
Bob Davis at 2007-11-9 0:49:11 >