【发布时间】: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++