• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Danicco

Heap Corruption with ifstream and delete[]

8 posts in this topic

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
0

Share this post


Link to post
Share on other sites
You're reading 3 bytes at a time and writing 4 bytes at a time.
2

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

[quote name='Danicco' timestamp='1356728125' post='5015177']


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?

[/quote]

 

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.

0

Share this post


Link to post
Share on other sites

[quote name='Bregma' timestamp='1356738355' post='5015229']
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.
[/quote]

 

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!

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0