NewToCPP wrote:
Is there any debugging mechanism to find out which part of the code is
causing memory leak?
Not in the C++ language itself. There are many third party utilities
that do so, but they are dependent upon your platform.
I suggest that you ask in a newsgroup more relevant to your platform,
such as either one of the microsoft.public.* newsgroups or
comp.unix.programmer, or whatever the mac programming newsgroups are.
There will be developers there who have dealt with this sort of thing,
and can tell you better what's available.
*** ON THE OTHER HAND ***
Many memory leaks can be avoided by not using dynamic memory if at all
possible, or by using containers and smart pointers.
e.g. If you have a pointer that points to a single instance, use the
smart pointer of your choice. If you have a pointer to multiple
elements, which you're using as a dynamic array, then try std::vector
instead. You should also use std::vector instead of arrays. If you're
allocating char* to use as strings, then you should be using std::string
instead.
By using these elements instead of trying to manage memory yourself, you
will minimize your leakage, because these containers (string, vector)
and smart pointers will manage the memory for you, freeing it when it is
no longer needed.
You should also google for the term RAII (or Resource Acquisition Is
Initialization).