【问题标题】:Checking for a memory leak?检查内存泄漏?
【发布时间】:2014-02-20 20:44:56
【问题描述】:

对于我的代码,expand 是将向量的容量加倍。它应该为动态分配的数组动态重新分配内存并更新容量的值,同时不会造成内存泄漏。

我想知道您将如何检查内存泄漏,因为我的测试未在 Visual Studio 中显示执行时间。

void IntVector::expand(){
    cap = cap * 2;
    int *data2;
    data2 = data;
    IntVector::~IntVector();
    data = new int[cap];
    data = data2;
    delete data2;
}

header(我知道您不应该使用命名空间 std)。

#ifndef INTVECTOR_H
#define INTVECTOR_H

using namespace std;
class IntVector{
private:
    unsigned sz;
    unsigned cap;
    int *data;
public:
    IntVector();
    IntVector(unsigned size);
    IntVector(unsigned size, int value);
    unsigned size() const;
    unsigned capacity() const;
    bool empty() const;
    const int & at (unsigned index) const;
    const int & front() const;
    const int & back() const;
    ~IntVector();
    void expand();

};

#endif

主文件

#include "IntVector.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;



IntVector::IntVector(){
    sz = 0;
    cap = 0;
    data = NULL;
}

IntVector::IntVector(unsigned size){
    sz = size;
    cap = size;
    data = new int[sz];
    *data = 0;
}

IntVector::IntVector(unsigned size, int value){
    sz = size;
    cap = size;
    data = new int[sz];
    for(int i = 0; i < sz; i++){
        data[i] = value;
    }
}

unsigned IntVector::size() const{
    return sz;
}

unsigned IntVector::capacity() const{
    return cap;
}

bool IntVector::empty() const{
    if(sz > 0){
        return false;
    }
    else{
        return true;
    }
}

const int &IntVector::at(unsigned index) const{
    if(index > sz){
        exit(1);
    }
    else{
        return data[index];
    }
}

const int &IntVector::front() const{
    return data[0];
}

const int &IntVector::back() const{
    return data[sz];
}

IntVector::~IntVector(){
    delete data;
}

void IntVector::expand(){
    cap = cap * 2;
    int *data2;
    data2 = data;
    IntVector::~IntVector();
    data = new int[cap];
    data = data2;
    delete data2;
}

编辑::

void IntVector::expand(){
    cap = cap * 2;
    int *data2 = data;
    data = new int[cap];
    delete[] data2;
    delete data2;
}

【问题讨论】:

  • 你为什么要data = data2;?这将使data 指向您随后销毁的旧数据。
  • 您还需要使用delete[] 来释放数组。而且你需要实现一个复制构造函数/赋值运算符。
  • 我打算存储新数组的本地地址,以免丢失旧数组。
  • 查找所有= new 并替换为make_uniquemake_sharedvector
  • void IntVector::expand(){ cap = cap * 2; int *data2 = 数据;数据=新的int [cap];删除[]数据2;删除数据2;这会改善原来的错误吗? *编辑:在原始帖子中发布我的编辑。

标签: c++


【解决方案1】:

这两行:

data = new int[cap];
data = data2;

分配一个整数数组,然后立即覆盖指向它的指针,从而永远失去分配的内存。那是内存泄漏。

使用 valgrind 或类似工具应该很容易导致这些错误。

【讨论】:

    【解决方案2】:

    在 Visual Studio 中测试内存泄漏:

    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    

    并且下一行将在代码中发生应用程序退出的每个位置自动显示内存泄漏报告。

    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    

    已为 maxywb 编辑

    【讨论】:

    • 您可能想提到这是特定于 Visual Studio 的。我知道 OP 正在寻找 VS 解决方案,但这样说是有礼貌的。
    【解决方案3】:

    我使用 Windows 的任务管理器。

    在 Windows 8 中: 详细信息选项卡 -> 右键单击​​列并“选择列” -> GDI 对象

    密切关注此列,如果它持续上升而它不应该上升,那么你就有泄漏。

    【讨论】:

    • 那只标识了极少数的内存泄漏,并没有发现问题中的泄漏
    猜你喜欢
    • 2011-05-21
    • 2012-01-02
    • 2010-10-09
    • 1970-01-01
    • 2012-07-16
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多