Thursday, February 28, 2019

c++ - Crash on reading file on seekg()



I am trying to read a bmp file into a character buffer and transfer it across processes using inter process communication. I accomplished this using following code:



     std::ifstream ImageFile;
char* str=new char[strlen(pFilePath)+strlen(pFileName)+1];
strcpy(str,pFilePath);
strcat(str,pFileName);
ImageFile.open(str, ios::binary);

if(ImageFile.is_open()){
ImageFile.seekg(0,ios::end);
m_uiImageSize = ImageFile.tellg();
ImageFile.seekg(0,ios::beg);

m_pcImageBuffer = new char[m_uiImageSize];
ImageFile.read(m_pcImageBuffer,m_uiImageSize);
ImageFile.close();
}


WebCore::FloatRect rect;
BITMAP cBitmap;
HBITMAP hBitmap;
tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)m_pcImageBuffer;
tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(m_pcImageBuffer+sizeof(tagBITMAPFILEHEADER));
RGBQUAD rgb = *(RGBQUAD*)(m_pcImageBuffer+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));

BITMAPINFO bi;
bi.bmiColors[0] = rgb;
bi.bmiHeader = bih;


char* pPixels = (m_pcImageBuffer+bfh.bfOffBits);
char* ppvBits;
hBitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
SetDIBits(NULL, hBitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);
GetObject(hBitmap, sizeof(BITMAP), &cBitmap);


This was working perfectly for me before. But now my program crashes at ImageFile.seekg(0,ios::end) and I cant figure out why. The file opens fine and even the ifstream::good() returns true. Following is my call stack:





StorageTree.exe!std::use_facet >(const std::locale & _Loc) Line 586 C++
msvcp110d.dll!std::basic_istream >::_Sentry_base::_Sentry_base(std::basic_istream > & _Istr) Line 103 C++
msvcp110d.dll!std::basic_istream >::sentry::sentry(std::basic_istream > & _Istr, bool _Noskip) Line 123 C++
msvcp110d.dll!std::basic_istream >::seekg(__int64 _Off, int _Way) Line 876 C++


Pls guide me in the right direction.


Answer



Found the solution to my problem. There wasn't any problem in the code, I had added some more code to the app and hadn't initialised some of my variables thus affecting the entry points and data locations.



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...