English 中文(简体)
在要求退出时,与病媒相传的不明确信息,即:
原标题:unclear memory leak with vector, c++, when calling exit

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:

#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:

==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

这是令人.慕的。 任何想法? 感谢

问题回答

<>代码>exit(>将不称为目前范围的变化者,因此可能出现泄漏:

(第3.6.1节) 要求履行以下职能:<代码> 避免退出(int);在<cstdlib> (18.3) 终止方案,而不必离开目前的组群,因此不销毁任何具有自动储存期限的物体(12.4)。 如果在销毁固定储存期限的物体时要求退出方案,则该方案没有界定行为。

而是使用:

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

病媒从来就没有消失。

exit(,主编,改为return 0;

我不认为你有记忆泄露。 当四舍五入表示记忆仍然可以传承时,它并没有告诉你它泄漏,而是说,在方案退出之前,它就没有放过。 在这种情况下,病媒脱轨器在离开前就被叫去。 从主线返回,而不是叫离开()。

您是否尝试将所有代码除exit外放在单独的<代码>{}栏中?

您不必称职,它会立即退出该方案,而没有打电话给监督厅。

途经是使用不离开()的回程。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签