简体   繁体   English

创建数组时C ++内存泄漏

[英]C++ memory leak when creating an array

In my C++ game I have an array of pointers to EnemyType s. 在我的C ++游戏中,我有一个指向EnemyType的指针数组。 I used Visual Leak Detector to detect memory leaks, and it's telling me I have a leak in the following piece of code: 我使用Visual Leak Detector来检测内存泄漏,并告诉我以下代码段中存在泄漏:

vector<EnemyType*> enemyTypes(number_of_lines);

for (int i = 0; i < number_of_lines; i++)
{
    enemyTypes[i] = new EnemyType();
}

Let's say number_of_lines is 3 in this case. 假设在这种情况下number_of_lines为3。 How is it possible that I'm creating a leak right here? 我怎么可能在这里制造泄漏? Can I do something about it? 我可以做些什么吗?

I started learning C++ about a month ago and I'm still learning every day, but I cannot understand some things (like this) without someone explaining me. 我大约一个月前开始学习C ++,但我仍然每天都在学习,但是如果没有人向我解释,我将无法理解某些东西(像这样)。

EDIT: I have modified the code to use a vector instead of a plain array. 编辑:我已经修改了代码以使用向量而不是纯数组。

From your original code: 从您的原始代码:

EnemyType** enemyTypes{ new EnemyType*[number_of_lines] };

for (int i = 0; i < number_of_lines; i++)
{
    enemyTypes[i] = new EnemyType();
}

When you allocate memory in C++ using new , you have to deallocate it using delete (or delete[] when you allocated an array, like your enemyTypes ). 当您使用new在C ++中分配内存时,必须使用delete (或在分配数组(例如enemyTypes )时使用delete[]来取消分配内存。

We see only part of your code, but I guess you're not doing any deallocation when you don't need the memory anymore. 我们只看到部分代码,但是我想当您不再需要内存时,您就不会进行任何释放。

By the way, you should just avoid allocating memory, or allocate it through managed pointers: 顺便说一句,您应该避免分配内存,或者通过托管指针分配内存:

#include <vector>
#include <memory>

// ...

std::vector<std::unique_ptr<EnemyType>> enemy_types(number_of_lines);

for (auto& enemy_type : enemy_types)
{
    enemy_type = std::make_unique<EnemyType>();
}

Depending on how you use your data, you can even avoid pointers: 根据使用数据的方式,甚至可以避免使用指针:

std::vector<EnemyType> enemy_types(number_of_lines);

Operator new dynamically allocates memory. 运算符new动态分配内存。

Some code, eventually, needs to release that memory using the corresponding operator delete . 最终,一些代码需要使用相应的运算符delete释放该内存。 If your code does not do that, the memory is never released. 如果您的代码不这样做,则永远不会释放内存。 That becomes a leak if your program loses track (eg the pointer used to hold the result of the new ceases to exist). 如果您的程序失去跟踪(例如,用于保存new结果的指针不再存在),那将成为泄漏。

A slightly modified version of your code 稍作修改的代码版本

int func()
{
    EnemyType** enemyTypes{ new EnemyType*[number_of_lines] };

    for (int i = 0; i < number_of_lines; i++)
    {
         enemyTypes[i] = new EnemyType();
    }
}

The pointer enemyTypes ceases to exist (since it passes out of scope) as the function returns. 当函数返回时,指针enemyTypes不再存在(因为它超出了范围)。 A consequence is that the memory created with the first usage of operator new is never released AND no pointer exists which points to it. 结果是,第一次使用运算符new创建的内存永远不会释放,并且不存在指向它的指针。 Since that memory cannot be accessed, the results of operator new in the loop also cannot be accessed (since the only place those results are stored is in the dynamically allocated array identified by enemyTypes ). 由于无法访问该内存,因此也无法访问循环中new运算符的结果(因为存储这些结果的唯一位置是在enemyTypes标识的动态分配的数组中)。

All that memory is therefore leaked at that point (the dynamically allocated memory cannot be recovered by your program, unless you use some technique outside the bounds of standard C++). 因此,所有内存在那一刻都会泄漏(除非您使用标准C ++范围以外的技术,否则程序无法恢复动态分配的内存)。 Various memory checkers - if you use them - will report a leak accordingly .... either at the point where the function returns, or when the program terminates. 各种内存检查器(如果使用它们)将在函数返回时或程序终止时相应地报告泄漏。

When using the operator new, verify that you delete each object that you created. 使用new运算符时,请确认已删除创建的每个对象。

A good alternative could be using smart pointers from STL or boost library. 一个很好的选择是使用STL或boost库中的智能指针。 They will delete automatically your object when it became unused, thus avoiding memory leaks. 当对象变得不使用时,它们将自动删除您的对象,从而避免内存泄漏。

原来我从来没有释放过内存(就像其他人告诉我的那样),但是Visual Leak Detector只是向我显示了该内存的分配位置,而不是泄漏发生的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM