【问题标题】:c++ incorrect checksum for freed object-c++ 释放对象的校验和不正确-
【发布时间】:2017-07-17 14:23:17
【问题描述】:

我收到此错误(内存位置因运行而异): 释放内存! Image_Processing(6282,0x100091000) malloc: * 对象 0x1212121212121212 的错误:未分配被释放的指针 * 在 malloc_error_break 中设置断点进行调试

此时它崩溃了: //delete m_data;

class Uint8Image {
public:
    uint32_t m_w;
    uint32_t m_h;
    uint8_t *m_data;


    Uint8Image(uint32_t w, uint32_t h): m_w(w), m_h(h), m_data(0)
    {
        m_data = new uint8_t(w*h);
    }

    Uint8Image(const Uint8Image &obj) ;

    Uint8Image& operator = (const Uint8Image &D ) {
        if(this != &D)
        {
            delete [] m_data;
            m_w= D.m_w;
            m_h = D.m_h;
            m_data=new uint8_t(m_w * m_h); // deep copy the pointer data
        }
        return *this;
    }

~Uint8Image()
    {
        std::cout << "Freeing memory!"<< std::endl;
    delete  m_data;   // it crashes here
    m_data = NULL;
    }

};

class MeniscusFinderContext {

public:

    MeniscusFinderContext( uint32_t m_a,  uint32_t m_b):

    {
      m_input_image = new Uint8Image(m_a,m_b);

    }

    ~MeniscusFinderContext()
    {
       delete m_input_image;
       m_input_image = NULL;

    }

Uint8Image m_input_image;};

//The function that calls:

// 通过选项解析获取输入,

int main(int argc, char *argv[]{
const char *file_name = options[INPUT].arg;

    std::ifstream file_stream(file_name,
                              std::ifstream::in | std::ifstream::binary);
    char buf[256];
    char *sEnd;
    file_stream.getline(buf, sizeof(buf));
    if(buf[0] != 'P' || buf[1] != '5') {
        std::cerr << "invalid input PGM file" << std::endl;
        return 1;
    }
    file_stream.getline(buf, sizeof(buf));
    while(buf[0] == '#') file_stream.getline(buf, sizeof(buf));
    uint32_t m_a = strtol(buf, &sEnd, 10);
    uint32_t m_b = strtol(sEnd, &sEnd, 10);

    MeniscusFinderContext M(m_a,m_b);


    file_stream.getline(buf, sizeof(buf));
    while(buf[0] == '#') file_stream.getline(buf, sizeof(buf));
    if(atoi(buf) != 255) return 3;
    file_stream.read((char *)M.m_input_image->m_data ,m_a * m_b);
    if(!file_stream) {
        std::cerr << "only got " << file_stream.gcount() << std::endl;
        return 2;
    }
    file_stream.close();
return 0;
}

编辑:我正在运行它,有时它会运行,而另一些它会给我错误。似乎是随机顺序。任何提示都会非常有帮助。 我已经检查了堆栈溢出中的所有相关答案,但无法弄清楚。

【问题讨论】:

  • 请尝试重新格式化您的帖子和代码,真的很难阅读。要在 Visual Studio 中重新格式化代码,您可以全选 (Ctrl+A) 然后按 CTRL+K+F
  • user2176127 现在还好吗?
  • m_data=new uint8_t(m_w * m_h); // deep copy the pointer data 不会复制任何内容。
  • @AshekDipro 为什么省略了UInt8Image 复制构造函数?您不能或不应该使用实施了三分之二的“3 规则”的类。

标签: c++


【解决方案1】:
new uint8_t(w*h);

这正好分配一个uint8_t,其初始值为w*h

你可能打算:

new uint8_t[w*h];

否则,这个:

file_stream.read((char *)M.m_input_image->m_data ,m_a * m_b);

将立即溢出此缓冲区。所示代码中多次出现相同的错误。

delete  m_data;

new[] 分配的东西应该用delete[] 释放。

一般来说,您的整体方法很容易出错。您应该使用std::vector 和迭代器,而不是手动处理内存。正确使用 C++ 的容器可以大大降低产生此类错误的可能性。

【讨论】:

  • 但我仍然收到此错误:Image_Processing(6371,0x100091000) malloc: *** 对象 0x100600268 错误:未分配指针被释放 *** 在 malloc_error_break 中设置断点以进行调试跨度>
猜你喜欢
  • 2015-03-08
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 2010-09-18
  • 2023-03-27
相关资源
最近更新 更多