The title says it all:

If I programmed without knowing it a memory leak, and the application terminates, is the memory of the memory leak freed?

+10  A: 

The OS executing your program usually does cleanup memory that is not freed explicitly and handles that are not closed explicitly, but this is not guaranteed by the C++ standard. You may find some embedded device that do not free up your memory leaks.

That being said Windows and all distros of Linux that I've ever seen do free up memory leaks.

You can easily create a huge loop of memory leaks though to test it out yourself. Watch your RAM usage grow and then close your program. You will see that the RAM usage goes back down.

Another consideration to consider when using C++ is that if you aren't deleting your heap allocated memory then your destructors are also not being called. Sometimes you will have other side effects as well if your destructors aren't called.

Brian R. Bondy
+13  A: 

Yes, a "memory leak" is simply memory that a process no longer has a reference to, and thus can no longer free. The OS still keeps track of all the memory allocated to a process, and will free it when that process terminates.


In the vast majority of cases the OS will free the memory - as is the case with normal "flavors" of Windows, Linux, Solaris, etc. However it is important to note that in specialized environments such as various Real-Time Operating Systems the memory may not be freed when the program is terminated.

Justin Ethier
Question: DOES the OS keep track of all allocations? Any heap memory you allocate comes from the virtual memory space of the process - when the process terminates, that space is given back to the system wholesale, right? I don't see why the extra bookkeeping would be necessary, since the process making the allocations is the only process (in userland) with access to those pages anyway. Or have I been mis-taught?
@Justin: Without knowing the OS I don't think this is valid. Maybe explicitly specify the OS you are talking about.
Brian R. Bondy
Note: the standard doesn't say anything about this. It's also not guaranteed. This is the way it works on most, if not all, modern OS's but they do not have to and in the past there were those that were both common and did not do this cleanup.
Noah Roberts
There are various real-time OS's for small devices and so forth that do not reclaim memory lost in leaks. Your question depends on the OS you are talking about.
Brian Neal
-1: Not true for all OS's
John Dibling
@Brian: Can you name example of OS, where that wouldn't be true?
@vartec The versions of vxWorks and pSOS+ I have used in the past are just two. They didn't have the concept of processes to begin with. Anything you took had to be freed by your program.
Brian Neal
@vartec: Nucleus RTOS... or at least some versions of it, don't reclaim leaked memory when the task that allocated the memory leaks it. There may be versions of Nucleus that include proper memory management where this isn't a problem (or maybe it's an add-on). Anyway, I've used a version of it where leaked memory was truly leaked until the system was reset.
Dan Moulding
Even with specific memory protected operating systems, you still need to define "leak". It's very possible for applications to allocate "system" resources outside their own memory space (handles are often used to identify these in the Windows world). Closing your application does not always ensure these types of resources are released.
+8  A: 

Are you running on a desktop OS (Windows, Linux etc.)? If so, yes, in general the system will free any memory associated with the program when the program exits.


Yeah :) It will freed by OS

All OS's aren't the same.
Brian Neal

Depends on what memory you leaked. Some memory can't be reclaimed by the OS. Most memory on most OSes however will be automatically reclaimed when the process exits.

Some memory can't be reclaimed by the OS - such as what?
On some real-time OS's, I've seen special Ethernet buffer blocks that don't get cleaned up when a process exits.
Brian Neal
Shared memory is also not usually reclaimed, because you may want it to persist for transient processes to use.
Brian Neal
+1  A: 

As far as I know, a modern operating system will free this memory once the program terminates.

+3  A: 

Usually, yes. Some systems support things like shared memory blocks that aren't automatically freed when a program exits though. Most still keep a reference count and delete it when all the programs that opened it exit, but a few don't (e.g., 16-bit Windows had a few types of items that would remain allocated even when nothing referred to them -- though it usually crashed for other reasons before enough of this accumulated to cause a problem...)

Jerry Coffin