[SOLVED] Bizarre FileRead Crash

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.

[SOLVED] Bizarre FileRead Crash

Postby xilefian » Fri Mar 09, 2012 12:16 am

I'm compiling and developing with visual C++ 2010 Express under the default environments (Debug and Release).

I added code to read a text file, it works perfectly file in Debug, but when I test under Release it crashes with:
Unhandled exception at 0x10138783 in Irrlicht Project.exe:
0xC0000005: Access violation reading location 0x00000005.

I stress that this does not happen under the Debugging environment.

My code is all in a main with this behavior:
createDevice()
getVideoDriver()
readMap() <- New Code that causes this problem
loadTextures() <- Section that Visual Studio breaks to
gameLoop()

My load map code is:
cpp Code: Select all
 
io::IReadFile *textfile = device->getFileSystem()->createAndOpenFile("map/test.txt");
 
            char ch[64] = {0};
 
        for (int z = 0; z < 64; z++)
        {
                for (int x = 0; x <= 64; x++)
                {
                        textfile->read(ch, 1);
                        if (ch[0] != '\n')
                        {
                                worldMap[64 - x][z] = atoi(ch);
                        }
                        else
                        {
                                textfile->read(ch, 1);
                                worldMap[64 - x][z] = atoi(ch);
                        }
                }
        }
 


And it breaks at the first loaded texture of three:
cpp Code: Select all
 
        device->getCursorControl()->setVisible(false);
 
        redbrick = driver->getTexture("pics/redbrick.png"); // Breaks here
        greystone = driver->getTexture("pics/wood.png");
        pillar = driver->getTexture("pics/greenlight2.png");
 


This is the next use of the file system after the map read.

If I move the map read to after the texture load, it breaks at the moment it meets my main loop:
cpp Code: Select all
 
        while (device->run() && quit == false) // Breaks here at device->run() as soon as it hits (Doesn't even loop once)
        {
              // Game code that works fine
        }
 


Again: It works under debug, not under release.

The map file I used and it's folder is in the working directory, the debug and the release folder, I have checked the working directory and it is normal and the same in both debug and release.

Oh yes, I get this on multiple machines and it is reproducible.

These are my includes:
cpp Code: Select all
 
#include <irrlicht.h>
#include <time.h>
 
using namespace irr;
using namespace scene;
 


EDIT: Changing the read length from 1 to 8 changes the error from 0x00000005 to 0x3131313d, so is this the issue?

EDIT 2: SOLVED!

The problem was that I was writing to my worldMap array a line break character (The test for the != '\n' when it returned false), the debugger must have shrugged this off but the release environment kept it strict and threw it back at me.
I've been scratching my head on this all day!
Simply removing the second worldMap[][] line from the map load fixed it.
xilefian
 
Posts: 9
Joined: Fri Mar 09, 2012 12:03 am

Re: [SOLVED] Bizarre FileRead Crash

Postby hendu » Fri Mar 09, 2012 8:47 am

Your loop also goes over the array, writing to the 65th position of a 64-element array.
hendu
 
Posts: 1556
Joined: Sat Dec 18, 2010 12:53 pm

Re: [SOLVED] Bizarre FileRead Crash

Postby xilefian » Fri Mar 09, 2012 11:32 am

hendu wrote:Your loop also goes over the array, writing to the 65th position of a 64-element array.

It runs under the assumption that every line of the file is 64 characters long and ends with a line break, so it needs to be 65 to read all 65 characters per line, and it ignores the line break when writing to the array, it's not clean code and it has huge room for error if the file is ever corrupted, but it is a rapidly developed project so it will do for the requirements
xilefian
 
Posts: 9
Joined: Fri Mar 09, 2012 12:03 am


Return to Beginners Help

Who is online

Users browsing this forum: No registered users and 1 guest