From what I understand, in standard C++ whenever you use the new operator you must also use the delete operator at some point to prevent memory leaks. This is because there
The long answer to it is that for every time new
is called, somewhere, somehow, delete
must be called, or some other deallocation function (depends on the memory allocator etc.)
But you don't need to be the one supplying the delete
call:
delete
to be called on an object, and all its children will automatically be delete
d as well.If you don't want to use any of these techniques, to safeguard against memory leaks, you can try using a memory checking tool. Valgrind is particularly good, although it only works on Linux
As for .NET, yes, allocating using gcnew
means that the memory is tracked by .NET, so no leaks. Other resources however, like file handles etc. are not managed by the GC.
Yes you are right, in standard C++ (In managed C++ or other variants it depends) you must use delete after each new. In C#, Java and other garbage-collected languages, this is not necessary (in fact most of them doesn't have an equivalent to the "delete" operator).