In an application of mine I basically allocate memory in C++
and enqueue it for deallocation in C#
. This deallocation runs in the background and is non-deterministic, so in very rare cases it can theoretically happen that the application is exited before all the unmanaged memory is deallocated.
If that is the case, the behaviour is (roughly and very stripped down) the same as if my program was
int main()
{
Foo* = new Foo();
return 0;
}
my questions now are
EDIT: This is about Windows only, as some mentioned that this is OS-dependent.
EDIT 2: I am not talking about simply ignoring all memory leaks in my application, but about whether I need to make sure that all memory is properly deallocated right before the application exits.
EDIT 3: This is not about open file handles, destructors and side effects or anything, this is about memory that will be non-deterministically deallocated and very rare cases where the memory is not freed before termination.
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.
The memory is reclaimed by the Operating system once your program exits. The OS doesn't understand that your program leaked memory, it simply allocates memory to the program for running and once the program exits it reclaims that memory.
Yes, it gets a contiguous chunk of virtual memory. This virtual memory is backed by physical memory as needed. I imagine once a program completes, all of this would be freed (freed = open for new program to consume).
Unlike stack memory, heap memory is allocated explicitly by programmers and it won't be deallocated until it is explicitly freed. To allocate heap memory in C++, use the keyword new followed by the constructor of what you want to allocate.
If you care only about memory, you might not need to call delete
because the operating system will destroy your entire virtual address space at process termination. Consider reading Operating Systems: Three Easy Pieces (freely downloadable) to understand more about OSes.
But you want to avoid memory leaks, so you'll better clean up properly. On some operating systems, you have tools like valgrind to help detect such leaks (so you don't want them to give spurious warnings).
Read also about RAII. In real life, the constructor (or other methods) of Foo
(or indirect data used by it) could consume other resources (database connections, opened files, windows on your screen, daemon processes, remote connections to external services or web servers, robotic arms, etc...) and you want them to be disposed of properly.
Consider using smart pointers.
Liveness of a data structure is a whole program property. I recommend reading the GC handbook, at least to understand the concepts and terminology of garbage collection (which can be viewed as a way to manage resources, and not only raw memory).
(In practice, it depends a lot: if you are coding a million-lines of code program with hundreds of other programmers, you need to be more careful than if you are coding a tiny single-source file program alone; if you code a neural surgery robot it is not the same as a desktop application, etc.... so YMMV)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With