我开发了一个小程序,它运行良好,直到我对代码的一些不相关部分进行了非常小的更改。从那时起,程序将引发未处理的 win32 异常,并且 Microsoft Visual Studio 即时调试器启动。
我正在使用代码块,我的编译器是 gcc 编译器。令人沮丧的是,如果我选择使用 gdb 从代码块进行调试,程序运行良好。这对我来说没有意义。
由于我无法使用 gdb 进行调试以查看问题所在(因为它在调试模式下运行良好),因此我将 printfs 放在此处和此处以查找所有问题的根源。我隔离了一个功能,但这没有任何意义。
bool FileReader::readBitmap(int fileNum)
{
char check;
int dataOffset;
int dataSize;
string fileName;
//used for quick int to string conversion
std::ostringstream stringstream;
stringstream<<fileNum;
string fileNumber = stringstream.str();
fileName = "img"+fileNumber+".bmp";
ifstream stream(fileName.c_str(),ios::in|ios::binary);
stream.read(&check,1);
//checking if it is a bitmap file
if(check != 'B')
return false;
stream.read(&check,1);
if(check != 'M')
return false;
stream.seekg(BMPBPP);
stream.read(&check,1);
//if it is not a monochrome bitmap
if(((int)check) != 1)
return false;//quit
//get the dataoffset
stream.seekg(DATAOFFSET);
stream.read(&check,1);
dataOffset = (int)check;
//get the data size in bytes
stream.seekg(DATASIZEINBYTES);
stream.read(&check,1);
dataSize = (int)check;
//if this is the first image we read
if(firstImageRead)
{
//allocate the image buffer
imgBuffer = (char*) malloc(dataSize);
//and make sure it does not get re-allocated
firstImageRead = false;
}
//get the actual bitmap data
stream.seekg(dataOffset);
stream.read(imgBuffer,dataSize);
stream.close();
return true;
}
-BIG- 编辑:试图找出问题所在,我将 ifstream 从函数移到成为类的私有成员。并且该函数现在的功能完全相同,只是它使用 stream.open() 打开文件。
现在它可以正常工作了。所以问题出在不知何故......每次在函数内部初始化ifstream,而不是仅仅在函数内部使用。仍然......没有意义,这不应该发生。
我真的很想知道这里的问题是什么?
老实说,有人知道这可以归因于什么吗?