Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this considered memory leak?

The general rule, only objects allocated in the free store can cause memory leaks. But objects created in the stack doesn't.

Here is my doubt,

int main()
    {
      myclass x;

      ...

      throw;

      ...
    }

If throw is not handled, it calls, terminate(), which in turn calls abort() and crashes the application. At this time, the objects in the stack are not destoryed (The destructor is not invoked).

My understanding is "When the application terminates (either by abort or by normal exit), it frees all the memory that was allocated for the application". Thus this cannot be considered as memory leak.

Am I correct?

like image 599
nsivakr Avatar asked Aug 25 '10 20:08

nsivakr


People also ask

How do you know if there is a memory leak?

Running out of memory is the simplest way to identify a memory leak, and it's also the most common approach to uncovering one. That's also the most inconvenient way to find a leak. You'll probably notice your system slowing down before you run out of RAM and crash your application.

Is memory leak a bug?

Memory leaks are a class of bugs where the application fails to release memory when no longer needed. Over time, memory leaks affect the performance of both the particular application as well as the operating system. A large leak might result in unacceptable response times due to excessive paging.

What are memory leaks?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.

What kind of test will identify memory leaks?

Any new illegal uses of memory and memory leaks are detected and reported through the CI system. The CI system's test reports provide transparency to a problem that is often undetected until it is too late. Generation of memory result files allows for follow-on detailed analysis of the application's use of memory.


1 Answers

In a hosted environment (e.g. your typical Unix / Windows / Mac OS X, even DOS, machine) when the application terminates all the memory it occupied is automatically reclaimed by the operating system. Therefore, it doesn't make sense to worry about such memory leaks.

In some cases, before an application terminates, you may want to release all the dynamic memory you allocated in order to detect potential memory leaks through a leak detector, like valgrind. However, even in such a case, the example you describe wouldn't be considered a memory leak.

In general, failing to call a destructor is not the same as causing a memory leak. Memory leaks stem from memory allocated on the heap (with new or malloc or container allocators). Memory allocated on the stack is automatically reclaimed when the stack is unwound. However, if an object holds some other resource (say a file or a window handle), failing to call its destructor will call a resource leak, which can also be a problem. Again, modern OSs will reclaim their resources when an application terminates.

like image 73
Diomidis Spinellis Avatar answered Oct 27 '22 00:10

Diomidis Spinellis