简体   繁体   English

C ++这些是否构成内存泄漏?

[英]C++ Do these constitute memory leaks?

I've been working on a simple ray tracer and I'm having issues with running out of memory. 我一直在研究一个简单的光线跟踪器,但内存不足时遇到了问题。 I downloaded Visual Leak Detector for visual studio and it told me the following functions are causing memory leaks. 我下载了用于Visual Studio的Visual Leak Detector,它告诉我以下功能导致内存泄漏。 I'm not sure why these would be considered leaks however: 我不确定为什么将这些视为泄漏:

Point* Point::copy() {

    Point* result = new Point(x, y, z);

    return result;
}

Point* Point::crossProduct(Point* other) {

    double crossX = y*(*other).getZ() - z*(*other).getY();
    double crossY = z*(*other).getX() - x*(*other).getZ();
    double crossZ = x*(*other).getY() - y*(*other).getX();

    Point* cross = new Point(crossX, crossY, crossZ);

    return cross;
}

Note I only discovered about copy constructors after creating and using the copy function shown here. 注意我仅在创建和使用此处显示的复制功能之后才发现有关复制构造函数的信息。 If I was going to redo the project I'd use a copy constructor instead. 如果要重做该项目,则应使用复制构造函数。 Now when I use the functions I make sure to call "delete" on whatever variable I'm using. 现在,当我使用函数时,请确保对我正在使用的任何变量调用“删除”。 For example: 例如:

Point* n = AB.crossProduct(&AC);
...
delete n;

Am I wrong in thinking that this should handle any memory leak? 我以为这应该处理任何内存泄漏是错误的吗? Is Visual Leak Detector just unable to recognize the leak has been handled because its in a separate file? Visual Leak Detector是否只是因为它在单独的文件中而无法识别已处理的泄漏?

Why not just return by value, and pass by const reference? 为什么不只按值返回并按const引用传递?

Point Point::copy() 
{
    return Point(x, y, z);
}

Point Point::crossProduct(const Point& other)
{
    double crossX = y * other.getZ() - z * other.getY();
    double crossY = z * other.getX() - x * other.getZ();
    double crossZ = x * other.getY() - y * other.getX();

    return Point(crossX, crossY, crossZ);
}

Of course your copy function is just a poor man's copy constructor/assignment operator, so use those instead: 当然,您的copy功能只是穷人的复制构造函数/赋值运算符,因此请改用这些函数:

Point::Point(const Point& other) 
    : x(other.x)
    , y(other.y)
    , z(other.z)
{
}

Point& Point::operator=(const Point& other) 
{
    x = other.x;
    y = other.y;
    z = other.z;
    return *this;
}

The rule is: 规则是:
Every dynamic memory allocation should have a corresponding deallocation. 每个动态内存分配都应有一个对应的释放。

Unless this rule is followed, you will end up with memory leaks. 除非遵循此规则,否则最终将导致内存泄漏。 Atleast any of the memory leak detection tools will detect them so. 至少任何一种内存泄漏检测工具都可以检测到它们。 There might be memory allocations which are never deallocated till end of lifetime of program but these instances will be very few. 可能存在一些内存分配,这些内存分配直到程序生命周期结束都不会释放,但是这些实例将很少。 And you shouldn't be dabbling in to those unless you really understand the concepts well. 除非您真的很了解这些概念,否则您不应该涉猎这些。

As for the simplest way to get memory allocations/deallocations this right is just to simply use Smart pointers . 至于获取内存分配/取消分配的最简单方法,此权利只是简单地使用智能指针

Note: Yes your handling seems correct based on the code you have shown. 注意:是的,根据显示的代码,您的处理似乎正确。

Rather than managing memory manually, use smart pointers . 与其手动管理内存,不如使用智能指针 This way, all the objects would be deallocated automatically, and you wouldn't have to worry about memory leaks. 这样,所有对象都将自动释放,而您不必担心内存泄漏。

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

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