C++ 删除类数组指针时出错
试图找出对此数组指针使用delete时出错的原因 正在尝试加载png图像,但不确定发生了什么 错误是使用C++ 删除类数组指针时出错,c++,windows,C++,Windows,试图找出对此数组指针使用delete时出错的原因 正在尝试加载png图像,但不确定发生了什么 错误是使用delete[]块时 代码 typedef struct { char r; char g; char b; char a; } pixel; class chunk { public: unsigned char length[4]; unsigned char type[4]; unsigned char* data; un
delete[]块时代码>
代码
typedef struct {
char r;
char g;
char b;
char a;
} pixel;
class chunk {
public:
unsigned char length[4];
unsigned char type[4];
unsigned char* data;
unsigned char CRC[4];
~chunk()
{
delete[] data;
}
};
class PNG
{
public:
PNG();
PNG(std::string filename);
~PNG();
void loadFile(std::string filename);
pixel* get();
private:
pixel * img;
int width;
int height;
int bitdepth;
int colourtype;
int compressionmethod;
int filtermethod;
int interlacemethod;
unsigned char* data;
std::ifstream* file;
int char2int(unsigned char* arr, int start);
};
void PNG::loadFile(std::string filename)
{
file = new std::ifstream(filename.c_str(), std::ios::in | std::ios::binary);
size_t size = 0;
file->seekg(0, std::ios::end);
size = file->tellg();
file->seekg(0, std::ios::beg);
data = new unsigned char[size];
file->read((char*)data, size);
/*
for (int i = 0; i < size; i++) {
std::cout << std::hex;
std::cout.width(2);
std::cout.fill('0');
std::cout << (int)data[i] << std::endl;
}
*/
size_t index = 8; // ignore header
chunk* chunks = new chunk[size];
size_t chunkindex = 0;
while (index < size) {
for (int i = 0; i < 4; i++) {
chunks[chunkindex].length[i] = data[index++];
}
std::cout << "Size of Chunk " << chunkindex + 1 << ": " << char2int(chunks[chunkindex].length, 0) << std::endl;
chunks[chunkindex].data = new unsigned char[char2int(chunks[chunkindex].length, 0)];
for (int i = 0; i < 4; i++) {
chunks[chunkindex].type[i] = data[index++];
}
if (char2int(chunks[chunkindex].length, 0) != 0) {
for (int i = 0; i < char2int(chunks[chunkindex].length, 0); i++) {
chunks[chunkindex].data[i] = data[index++];
}
}
for (int i = 0; i < 4; i++) {
chunks[chunkindex].CRC[i] = data[index++];
}
chunkindex++;
}
for (int i = 0; i < chunkindex; i++) {
char name[5];
for (int j = 0; j < 4; j++) {
name[j] = chunks[i].type[j];
}
name[4] = '\0';
if (strcmp(name, "IHDR") == 0) {
std::cout << "FOUND IHDR" << std::endl;
width = char2int(chunks[i].data, 0);
height = char2int(chunks[i].data, 4);
bitdepth = chunks[i].data[8];
colourtype = chunks[i].data[9];
compressionmethod = chunks[i].data[10];
filtermethod = chunks[i].data[11];
interlacemethod = chunks[i].data[12];
}
else if (strcmp(name, "PLTE") == 0) {
std::cout << "FOUND PLTE" << std::endl;
}
else if (strcmp(name, "IDAT") == 0) {
std::cout << "FOUND IDAT" << std::endl;
}
else if (strcmp(name, "IEND") == 0) {
std::cout << "FOUND IEND" << std::endl;
}
}
std::cout << "Width: " << width << std::endl;
std::cout << "Height: " << height << std::endl;
delete[] chunks;
}
typedef结构{
字符r;
charg;
字符b;
字符a;
}像素;
类块{
公众:
无符号字符长度[4];
无符号字符类型[4];
无符号字符*数据;
无符号字符CRC[4];
~chunk()
{
删除[]数据;
}
};
类PNG
{
公众:
PNG();
PNG(std::字符串文件名);
~PNG();
无效加载文件(std::字符串文件名);
像素*get();
私人:
像素*img;
整数宽度;
内部高度;
整数位深度;
int颜色类型;
内压缩法;
int滤波法;
int层间方法;
无符号字符*数据;
std::ifstream*文件;
int char2int(无符号字符*arr,int start);
};
void PNG::loadFile(std::string文件名)
{
file=newstd::ifstream(filename.c_str(),std::ios::in | std::ios::binary);
大小\u t大小=0;
文件->seekg(0,std::ios::end);
大小=文件->tellg();
文件->seekg(0,std::ios::beg);
数据=新的无符号字符[大小];
文件->读取((字符*)数据,大小);
/*
对于(int i=0;i std::cout将默认构造函数添加到块
类中
chunk::chunk () {
data = 0;
}
当你打电话的时候
chunk* chunks = new chunk[size];
delete[] chunks;
size
对象已创建,并且所有创建的对象在data
变量中都有垃圾数据-此成员未初始化。当您调用
chunk* chunks = new chunk[size];
delete[] chunks;
对于chunks
数组的每个对象,会调用dtor,但当数据
成员未设置时-删除[]数据
是未定义的行为
未为块
数组中的所有对象设置数据
成员-请参见while循环:为此行中的数据
成员分配内存
chunks[chunkindex].data = new unsigned char[char2int(chunks[chunkindex].length, 0)];
但并非针对所有创建的对象,因为在while循环中,每次迭代索引
都会增加多次,但chunkindex
仅增加一次。当while循环被此contidion索引
停止时,chunkindex
不等于大小
这意味着存在具有单位化数据
成员。将默认构造函数添加到块
类中
chunk::chunk () {
data = 0;
}
当你打电话的时候
chunk* chunks = new chunk[size];
delete[] chunks;
size
对象已创建,并且所有创建的对象在data
变量中都有垃圾数据-此成员未初始化。当您调用
chunk* chunks = new chunk[size];
delete[] chunks;
对于chunks
数组的每个对象,会调用dtor,但当数据
成员未设置时-删除[]数据
是未定义的行为
未为块
数组中的所有对象设置数据
成员-请参见while循环:为此行中的数据
成员分配内存
chunks[chunkindex].data = new unsigned char[char2int(chunks[chunkindex].length, 0)];
但并非针对所有创建的对象,因为在while循环中,每次迭代索引
都会增加多次,但chunkindex
仅增加一次。当while循环被此contidion索引
停止时,chunkindex
不等于大小
这意味着存在具有单位化数据
成员。您不需要在代码中使用所有这些指针。在我看来,对于该应用程序,您应该求助于std::vector
来帮助您进行内存管理。而且,您确实存在内存泄漏——您正在分配std::ifstream
对象,而不是使用匹配的delete来释放它代码>。好吧,也许你是在PNG
的析构函数中完成的,我们看不到,但无论如何,你并没有关闭你打开的文件。你应该将资源(在本例中是打开的文件)释放回系统
对于粘贴的代码摘录,您可能应该有类似于以下内容的内容,将裸指针更改为std::vector
s,并将文件
成员更改为对象而不是指针:
#包括
#包括
#包括
#包括
类型定义结构{
字符r;
charg;
字符b;
字符a;
}像素;
类块{
公众:
无符号字符长度[4];
无符号字符类型[4];
//无符号字符*数据;
/*让std::vector解决内存问题*/
std::矢量数据;
无符号字符CRC[4];
//~chunk(){delete[]data;}
};
类PNG{
公众:
PNG();
PNG(std::字符串文件名);
~PNG();
无效加载文件(std::字符串文件名);
像素*get();
私人:
//像素*img;
/*帮助的向量*/
std::向量img;
整数宽度;
内部高度;
整数位深度;
int颜色类型;
内压缩法;
int滤波法;
int层间方法;
/*求你了*/
//无符号字符*数据;
std::矢量数据;
//std::ifstream*文件;
/*这里不需要指针*/
std::ifstream文件;
int char2int(无符号字符*arr,int start);
};
void PNG::loadFile(std::string文件名){
file=std::ifstream(文件名,std::ios::in | std::ios::binary);
/*最好检查文件是否正确打开*/
//如果(文件){…}
大小\u t大小=0;
seekg(0,std::ios::end);
size=file.tellg();
seekg(0,std::ios::beg);
数据=标准::向量(大小);
file.read(重新解释强制转换(&data[0]),大小);
/*显然你已经处理完文件了*/
file.close();
/*
对于(int i=0;i std::cout您不需要在代码中使用所有这些指针。在我看来,对于该应用程序,您应该求助于std::vector
来帮助您进行内存管理。而且,您确实存在内存泄漏——您正在分配std::ifstream
对象,而不是使用匹配的delete
来释放它。好吧,可能您是在PNG
的析构函数中执行此操作,我们看不到,但无论如何,您并没有关闭已打开的文件。您应该将资源(在本例中,是打开的文件)释放回系统
对于你粘贴的代码摘录,也许