Fuzzy memory leak with a vector, C ++, when calling exit

I was debugging my program, and I noticed that although I marked almost all of this as a comment, and all I did was insert double values ​​into the vector, I have a memory leak. I read the api in the C ++ link, but found nothing. Here is the code:

#include <vector> #include <cstdlib> #include <iostream> #include "RegMatrix.h" #include "Matrix.h" using namespace std; int main(void) { vector<double> v; for (int i=0; i<9; i++) { v.push_back(i); } cout << endl; exit(EXIT_SUCCESS); } 

And the valgrind message:

 ==9299== HEAP SUMMARY: ==9299== in use at exit: 128 bytes in 1 blocks ==9299== total heap usage: 5 allocs, 4 frees, 248 bytes allocated ==9299== ==9299== 128 bytes in 1 blocks are still reachable in loss record 1 of 1 ==9299== at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255) ==9299== by 0x804937D: __gnu_cxx::new_allocator<double>::allocate(unsigned int, void const*) (in /home/yotamoo/workspace/ex3/main) ==9299== by 0x804922F: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (in /home/yotamoo/workspace/ex3/main) ==9299== by 0x8048E6C: std::vector<double, std::allocator<double> >::_M_insert_aux(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, double const&) (in /home/yotamoo/workspace/ex3/main) ==9299== by 0x8048CA2: std::vector<double, std::allocator<double> >::push_back(double const&) (in /home/yotamoo/workspace/ex3/main) ==9299== by 0x8048B10: main (in /home/yotamoo/workspace/ex3/main) ==9299== ==9299== LEAK SUMMARY: ==9299== definitely lost: 0 bytes in 0 blocks ==9299== indirectly lost: 0 bytes in 0 blocks ==9299== possibly lost: 0 bytes in 0 blocks ==9299== still reachable: 128 bytes in 1 blocks ==9299== suppressed: 0 bytes in 0 blocks 

This is strange. Any ideas? thanks

+7
source share
5 answers

exit() will not call the destructors of the current scope, so a leak may occur:

(Β§3.6.1 / 4) Calling the function void exit(int); , declared in <cstdlib> (18.3), terminates the program without leaving the current block and, therefore, without destroying any objects with automatic storage time (12.4). If a call is made to terminate a program while destroying an object with a static storage duration, the program has undefined behavior.

Use this instead:

 #include <vector> #include <iostream> int main(int argc, char *argv[]) { std::vector<double> v; for (int i=0; i<9; i++) { v.push_back(i); } std::cout << endl; return 0; } 
+18
source

A vector never goes beyond an exit.

Just remove exit() from main and replace it with return 0;

+7
source

I do not believe that you have a memory leak. When valgrind says that the memory is still available, it does not tell you that it leaked, but that it was not free until the program exited. In this case, the vector descriptor was not called before exiting. Try returning from the main one rather than calling exit ().

+2
source

Have you tried to put all the code except exit in a separate block {} ?

+1
source

You did not need to call the exit function, from which it immediately exited the program, did not cause OS cleanup calls.

Always use return () not exit ().

0
source

All Articles