调用exit时,与向量c++的内存泄漏不清

时间:2022-09-05 18:46:43

I was debugging my program and I've noticed that even though I've marked almost all of it as comment and all I did was to push double values into a vector, I have a memory leak. I read the api in c++ reference, but couldn't find anything. Here's the code:

我正在调试我的程序,我注意到,尽管我几乎把它都标记为注释,我所做的只是将双值推入一个向量,但我有一个内存泄漏。我阅读了c++引用中的api,但是什么也找不到。这是代码:

#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 valgrind's report:

和valgrind的报告:

==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 weird. Any ideas? thanks

这是奇怪的。什么好主意吗?谢谢

5 个解决方案

#1


18  

exit() will not call the destructors of the current scope thus there may be a leak:

exit()不会调用当前作用域的析构函数,因此可能会出现泄漏:

(§3.6.1/4) Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior.

(§3.6.1/4)调用函数无效退出(int);在 (18.3)中声明,在不离开当前块的情况下终止程序,因此不会破坏任何具有自动存储持续时间的对象(12.4)。如果在使用静态存储期间销毁对象期间调用exit来结束程序,则该程序具有未定义的行为。

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;
}

#2


7  

The vector never goes out of scope for the exit.

矢量永远不会超出出口的范围。

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

只需从main中删除exit(),并用返回0替换它;

#3


2  

I don't believe that you have a memory leak. When valgrind says the memory is still reachable it's not telling you that it leaked but that it wasn't free'ed before the program exited. In this case the vector desctructor didn't get called before exit. Try returning from main rather than calling exit().

我不相信你有内存泄漏。当valgrind说内存仍然是可访问的它并不是说它泄漏了,而是说它在程序退出之前并没有释放。在这种情况下,向量desctructor在退出之前没有被调用。尝试从main返回,而不是调用exit()。

#4


1  

Did you try putting all the code except exit in a separate {} block?

您是否尝试将除exit之外的所有代码放在一个单独的{}块中?

#5


0  

You did not have to call the exit function it will immediate exit from the program did not call the OS clean up calls.

您不必调用退出函数,它将立即退出程序,并没有调用操作系统清理调用。

Always use the return() not exit().

总是使用return()而不是exit()。

#1


18  

exit() will not call the destructors of the current scope thus there may be a leak:

exit()不会调用当前作用域的析构函数,因此可能会出现泄漏:

(§3.6.1/4) Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior.

(§3.6.1/4)调用函数无效退出(int);在 (18.3)中声明,在不离开当前块的情况下终止程序,因此不会破坏任何具有自动存储持续时间的对象(12.4)。如果在使用静态存储期间销毁对象期间调用exit来结束程序,则该程序具有未定义的行为。

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;
}

#2


7  

The vector never goes out of scope for the exit.

矢量永远不会超出出口的范围。

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

只需从main中删除exit(),并用返回0替换它;

#3


2  

I don't believe that you have a memory leak. When valgrind says the memory is still reachable it's not telling you that it leaked but that it wasn't free'ed before the program exited. In this case the vector desctructor didn't get called before exit. Try returning from main rather than calling exit().

我不相信你有内存泄漏。当valgrind说内存仍然是可访问的它并不是说它泄漏了,而是说它在程序退出之前并没有释放。在这种情况下,向量desctructor在退出之前没有被调用。尝试从main返回,而不是调用exit()。

#4


1  

Did you try putting all the code except exit in a separate {} block?

您是否尝试将除exit之外的所有代码放在一个单独的{}块中?

#5


0  

You did not have to call the exit function it will immediate exit from the program did not call the OS clean up calls.

您不必调用退出函数,它将立即退出程序,并没有调用操作系统清理调用。

Always use the return() not exit().

总是使用return()而不是exit()。