Yes a memory leak can be the lesser of two evils. Whilst correctness is important, the performance, or the stability of the system can be impacted when performing full memory release, and the risks and time spent freeing memory and destroying objects may be less desirable than just exiting a process.
In general, it is not usually acceptable to leave memory around. It is difficult to understand all of the scopes which your code will run in, and in some cases, it can result in the leak becoming catastrophic.
What if you allocate some memory and use it until the very last line of code in your application (for example, a global object's destructor)?
In this case, your code may be ported within a larger project. That may mean the lifetime of your object is too long (it lasts for the whole of the program, not just the instance where it is needed), or that if the global is created and destroyed, it would leak.
is it OK to trust the OS to free your memory for you when your application terminates
When a short lived program creates large C++
collections (e.g. std::map
), there are at least 2 allocations per object. Iterating through this collection to destroy the objects takes real time for the CPU, and leaving the object to leak and be tidied up by the OS, has performance advantages. The counter, is there are some resources which are not tidied by the OS (e.g. shared memory), and not destroying all the objects in your code opens the risk that some held onto these non-freed resources.
What if a third party library forced this situation on you?
Firstly I would raise a bug for a close
function which freed the resources. The question on whether it is acceptable, is based on whether the advantages the library offers (cost, performance, reliability) is better than doing it with some other library, or writing it yourself.
In general, unless the library may be re-initialized, I would probably not be concerned.
acceptable times to have a reported leak memory.
- A service during shutdown. Here there is a trade-off between time performance and correctness.
- A broken object which can't be destroyed. I have been able to detect a failed object (e.g. due to exception being caught), and when I try and destroy the object the result is a hang (held lock).
- Memory checker mis-reported.
A service during shutdown
If the operating system is about to be turned off, all resources will be tidied up. The advantage of not performing normal process shutdown, is the user gets a snappier performance when turning off.
A broken object
Within my past, we found an object (and raised a defect for that team), that if they crashed at certain points, they became broken, that all of the subsequent functions in that object would result in a hang.
Whilst it is poor practice to ignore the memory leak, it was more productive to shutdown our process, leaking the object and its memory, than to result in a hang.
leak checker mis-reporting
Some of the leak checkers work by instrumenting objects, and behaving in the same way as globals. They can sometimes miss that another global object has a valid destructor, called after they finish which would release the memory.