Jump to content

  • Log In with Google      Sign In   
  • Create Account


Heap Corruption with ifstream and delete[]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 28 December 2012 - 02:28 PM

I have this code:

 



ifstream reader;
reader.open("myFile.bmp", ios::binary);

//Read the headers and stuff, fill some variables
unsigned char *bitmapArray = new unsigned char[imageWidth * imageHeight * 4]; //array with enough values for RGBA of the BMP

int c = 0;
while(!reader.eof())
{
    reader.read(pixelInfo, 3); //pixelInfo is a char[3]

    //Filling RGBA info into my bitmapArray
    bitmapArray[c] = pixeInfo[0];
    bitmapArray[c + 1] = pixeInfo[1];
    bitmapArray[c + 2] = pixeInfo[2];
    bitmapArray[c + 3] = 255; 
    c += 4; //increasing C for the next values
}
reader.close();

//Here I use bitmapArray for some stuff

delete[] bitmapArray; //This gives me HEAP CORRUPTION error

If I don't mess with bitmapArray inside the while loop (simply commenting 4 lines, still leaving the reader there), it goes through fine.

The size of the image I'm using is 40000 bytes long, if I declare the array as "unsigned char bitmapArray[40000];" I still get the same error, but if I add a single char it doesn't - "unsigned char bitmapArray[40001];"

I checked the address the debugger says it's corrupted, but it isn't nowhere close to the starting/ending addresses of bitmapArray.

 

The error is: "HEAP CORRUPTION DETECTED: after Normal block (#161) at 0x00413F58. CRT detected that the application wrote to memory after end of heap buffer."

 

Why is this happening?


Edited by Danicco, 28 December 2012 - 02:53 PM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9382

Like
2Likes
Like

Posted 28 December 2012 - 02:33 PM

You're reading 3 bytes at a time and writing 4 bytes at a time.

#3 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 28 December 2012 - 02:55 PM

You're reading 3 bytes at a time and writing 4 bytes at a time.

 

Ops, typed without thinking, didn't notice that.

 

I managed to find what is causing the HEAP CORRUPTION, it's because of the cast from signed char to unsigned char in the while loop, if I use a signed char array it works fine only to go through the debugger, but I can't use signed char for the rest of the code. Is there any solution for this?


Edited by Danicco, 28 December 2012 - 02:55 PM.


#4 Paradigm Shifter   Crossbones+   -  Reputation: 5112

Like
3Likes
Like

Posted 28 December 2012 - 04:06 PM

A .bmp file has a header (think it may be a BITMAPINFO structure but it may be something else, even variable size). Also, each row of a bmp is padded to a multiple of 4 bytes, you need to check the stride variable from the header and reading one entire line at a time is probably best.

Also you need to remember bmp files are stored upside down, for arcane reasons ;)

EDIT: If it is a palettised bitmap, the palette appears after the header. Also, your code won't work if it reads a 32 bit RGBA bitmap, so you need to check the header.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#5 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

Posted 28 December 2012 - 04:45 PM

I suggest you read up on the BMP file format. For what it's worth, BITMAPINFOHEADER is the most common DIB header, and supporting the other DIB headers is usually overkill/unnecessary.


[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#6 alnite   Crossbones+   -  Reputation: 2038

Like
0Likes
Like

Posted 28 December 2012 - 05:31 PM

I managed to find what is causing the HEAP CORRUPTION, it's because of the cast from signed char to unsigned char in the while loop, if I use a signed char array it works fine only to go through the debugger, but I can't use signed char for the rest of the code. Is there any solution for this?

 

I doubt casting from signed to unsigned could cause a heap corruption (unless the overflow from signed to unsigned mess up your calculation).  When it comes to debugging heap corruption, do not rely on results alone.  Corrupted heap is one of those things that may produce different results when run at different times under different conditions, and you can't trust what the debugger says as it's likely to point at a totally different place in the code and thinks that's where the problem is, while in reality it's somewhere else..  The only solution is to reinspect all your code and unit test them.



#7 Bregma   Crossbones+   -  Reputation: 4747

Like
4Likes
Like

Posted 28 December 2012 - 05:45 PM

If I were a gambler, my money would rest on the fact that you do not check the success of the read() call, and the fact that ifstream::eof() doesn't work they way you're using it.

 

At a guess, this might work better.

//.. preamble

int c = 0;
while(reader.read(pixelInfo, 3)) //pixelInfo is a char[3]
{
  //Filling RGBA info into my bitmapArray
  bitmapArray[c] = pixeInfo[0];
  bitmapArray[c + 1] = pixeInfo[1];
  bitmapArray[c + 2] = pixeInfo[2];
  bitmapArray[c + 3] = 255;
  c += 4; //increasing C for the next values
}
reader.close();

//Here I use bitmapArray for some stuff

 


Stephen M. Webb
Professional Free Software Developer

#8 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 28 December 2012 - 06:24 PM

If I were a gambler, my money would rest on the fact that you do not check the success of the read() call, and the fact that ifstream::eof() doesn't work they way you're using it.

 

Yep, that was it, although I did:

 

while(c < bitmapArrayLength)
{
    //etc
}

It seems the reader.eof() was writing beyond the array bounds and attempting to delete it was messing everything up.

 

Thanks for the help!



#9 Khatharr   Crossbones+   -  Reputation: 2817

Like
0Likes
Like

Posted 28 December 2012 - 08:50 PM

Aye, you always want to try to have a direct relationship between the number that fills a buffer and the number that was used to create it.

 

+1 Bregma

 

Meanwhile, you're doing a bit of extra work here, so I thought I'd make a suggestion:

 

You're reading into the array 'pixelInfo', but your real target is the bitmap data. Why not try something like:

 

unsigned char* texelPtr = bitmapArray; //roaming pointer that will traverse the data
unsigned char* texelEnd = texelPtr + (the length of the array); //this pointer value indicates the first overflow byte

while(texelPtr < texelEnd) {
    reader.read(texelPtr, 3);
    texelPtr[3] = 255;
    texelPtr += 4;
}

Which would eliminate the double-copy behavior?


Edited by Khatharr, 28 December 2012 - 08:52 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS