【问题标题】:Unable to find simple memory leak [closed]找不到简单的内存泄漏[关闭]
【发布时间】:2013-01-21 17:37:25
【问题描述】:

有人可以帮我找出这里发生的内存泄漏吗?我只是尝试使用我设计的 Image 类将 1600x960 24 位 RAW 图像(46,08,000 字节)加载到内存中。正如我在任务管理器中看到的那样,在内存中它占用了 30MB..

即使在调用析构函数(超出范围)之后,它仍然占用 2M。请帮忙!

#include <cstdio>
#include <iostream>

struct pixel {
    char* color;  // to support various BPP
};

class Image
{
    private:
        pixel** image;
        int     width;
        int     height;
        int     BPP;    // bytes per pixel
        int     size;   // in bytes

    public:
        Image(std::string src, int width, int height, int BPP);
        ~Image();
        pixel** get_matrix(int col, int row, int BPP);
};

pixel** Image :: get_matrix(int col, int row, int BPP)
{
            pixel** matrix = new pixel*[row];
            for(int i=0 ; i<row ; i++)
            {
                matrix[i] = new pixel[col];
                for(int j=0 ; j<col ; j++)
                    matrix[i][j].color = new char[BPP];
            }
            return matrix;
}

Image :: Image(std::string src, int width, int height, int BPP)
{
    FILE *in;
    if( (in = fopen(src.c_str(), "rb")) == NULL )
        image = NULL;
    else
    {
        this->height = height;
        this->width  = width;
        this->BPP    = BPP;
        this->size   = width*BPP*height;

        image = get_matrix(width,height,BPP);
        char* buffer = new char[size];
        fread(buffer, sizeof(char), size, in);

        int l=0;
        for(int i=0 ; i<height ; i++)
        {
            for(int j=0 ; j<width ; j++)
            {
                for(int k=0 ; k<BPP ; k++)
                    image[i][j].color[k] = buffer[l++];
            }
        }
        delete []buffer;
        fclose(in);
    }
}

Image :: ~Image()
{
    for(int i=0 ; i<height ; i++)
    {
        for(int j=0 ; j<width ; j++)
            delete []image[i][j].color;
        delete []image[i];
    }
    delete []image;
}

int main()
{
    {
        getchar();
        Image in("x.raw", 1600, 960, 3);
        getchar();
    }
    getchar();
}

【问题讨论】:

  • 1) 你确定这是内存泄漏吗?你用的是什么工具? 2) 您可以省略文件 I/O,将颜色初始化为 {0,0,0} 并简化操作。
  • @Beta 我在 Win7 上使用 Codeblocks,使用 Process Explorer 查看内存使用情况。我会试试你说的
  • 我也没有看到任何泄漏。如果您仍然在处理该类,则应将字符串参数作为参考传递给构造函数,即const std::string&amp; src。现在,您正在制作不必要的副本。我之所以提到这一点,是因为传递副本而不是引用很快就会成为一个坏习惯。
  • Can someone please help me find the memory leak which is occurring here? 不。这是一个问答网站,不是调试众包网站。
  • 我可以帮你修复泄漏:使用std::vector

标签: c++ memory-leaks


【解决方案1】:

我无法发现那里的内存泄漏,但程序在内存方面相当浪费:

  1. 加载时,将整个文件加载到内存中,然后构造矩阵。在加载结束时,文件和矩阵都在内存中。如果格式允许,它可以尝试迭代加载文件(例如逐行)。

  2. 图像矩阵存储格式是数组数组的数组。由于每个维度上的数组是单独分配的,并且对于每个分配的数组,都有一定数量的内存(通常为 8-16 字节)用于内存分配器内部,因此这种存储矩阵的方式会浪费大量内存。尝试使用普通的std::vector&lt;&gt;,例如理想情况下:

    struct RGB24 { uint8_t r, g, b; }; // one for each pixel format
    std::vector<RGB24> image(width * height); // allocate the matrix in one shot
    RGB24& pixel = image[row * width + col]; // get pixel image[row][col] 
    

【讨论】:

  • 明白了,谢谢!所以我不会通过使用 std::vector 来浪费任何内存?而且,对于我的简单目的,它会很快吗?
  • @Bruce 与数组相比,使用向量应该没有内存差异。
  • 请不要通过回答这些“问题”来鼓励他们:(
猜你喜欢
  • 2017-11-14
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 2012-04-03
相关资源
最近更新 更多