I just wrote C ++ code that does some string manipulation, but when I ran valgrind it shows some possible memory leaks. Debugging code to the granular level I wrote a simple C ++ program that looks like this:
#include<iostream> #include<cstdlib> using namespace std; int main() { std::string myname("Is there any leaks"); exit(0); }
and running valgrind over it, I got:
==20943== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 26 from 1) ==20943== malloc/free: in use at exit: 360,645 bytes in 12,854 blocks. ==20943== malloc/free: 65,451 allocs, 52,597 frees, 2,186,968 bytes allocated. ==20943== For counts of detected errors, rerun with: -v ==20943== searching for pointers to 12,854 not-freed blocks. ==20943== checked 424,628 bytes. ==20943== ==20943== LEAK SUMMARY: ==20943== definitely lost: 0 bytes in 0 blocks. ==20943== possibly lost: 917 bytes in 6 blocks. ==20943== still reachable: 359,728 bytes in 12,848 blocks. ==20943== suppressed: 0 bytes in 0 blocks. ==20943== Reachable blocks (those to which a pointer was found) are not shown. ==20943== To see them, rerun with:
Then it seemed to me that we were forced to exit (which I also performed in my C ++ source code). Now the problem is that I want to exit the program, since my previous old code is waiting for the exit status of the new code. For example, the a.out binary is awaiting the exit status of b.out. Is there a way to avoid memory leaks, or do I really need to worry about a memory leak, since the program is already exiting from this point.
This also begs me another question, is such code harmful?
#include<stdio.h> #include<cstdlib> int main() { char *p=(char *)malloc(sizeof(char)*1000); exit(0); }
c ++ c memory-leaks valgrind
Global Warrior Jan 27 '12 at 13:27 2012-01-27 13:27
source share