Clueless on error

Hi all, forgive me if this is a newbie problem but I can't figure it out. What I'm doing is opening a file, then trying to read it in. I get an error when I try to do the read.

"An unknown error occurred while accessing an unnamed file."

I've traced the problem to this function

ASSERT(AfxIsValidAddress(void*, uint));

From what I've read about this is it checks to make sure there's enough memory? Here's my code anyway, you'll see what I'm doing. Maybe some of you gurus have seen this before. Thanks.

CFile dibFile;
//open file
if(!dibFile.Open(_T("BITMAP1.bmp"), CFile::modeRead))
AfxMessageBox("Didn't open file");
//create memory for teh file header
//read the header into memory
if(!dibFile.Read((void*)&bmpFileHead, sizeof(BITMAPFILEHEADER)))
AfxMessageBox("Couldn't read the file");
//check to make sure the file is a valid bitmap
if(bmpFileHead.bfType == 0x4d42)
AfxMessageBox("Is a bmp");

//need the length of the file for further processing

DWORD fileLength = dibFile.GetLength();

//take the size of the header away from the length, we already have the header in memory

DWORD size = fileLength - sizeof(BITMAPFILEHEADER);

//create a pointer to the bitmap data as a chunk of memory

BYTE* pDib = (BYTE*)::GlobalAlloc(GMEM_MOVEABLE, size);
int x = ::GetLastError();
SIZE_T y = GlobalSize(pDib);
get last error returns 0, so the memory is being allocated, and GlobalSize is returning the same number as size. that means the memory is definately allocated right??
//read the data into the array
[green]//this next line is causing the error
//I followed the program into the CFile::Read function
//where AfxIsValidAddress is checked. [/green]
dibFile.Read((void*)pDib, size); [/red]


  • Ok, I've figured out my problem to an extent. I was reading past the end of the file. Instead of using size, I tried 100. But this doesnt' make sense to me. I thought that once you read from a file, there's a pointer that moves to the last byte read. Then if you read again, it starts from there? Obviously I missread that, because if that was true my code should work, shouldnt' it?
  • found it. thanks anyway. hehe guess I should have tried a little harder before posting.

    Anyway, turns out the memory was bad for reading. Not really sure why though. But I changed from GlobalAlloc to malloc and it works perfectly. Still have some issues when rewriting to the new file though.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion