Problems with reading files when shifting among scenes

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.
Post Reply
winny8674
Posts: 4
Joined: Fri Feb 22, 2008 11:54 am

Problems with reading files when shifting among scenes

Post by winny8674 »

Hi,

I got a problem when I am reading data from a text files when shifting from scenes to scenes.

What I write in retrieving data from file is

Code: Select all

char temp[255];
for(int i = 0; i < n; i++){
        in.getline(temp, 31);
        name[i] = irr::core::string<irr::c8>(temp); ..... (*)
and name is defined as

Code: Select all

irr::core::string<irr::c8> name[50];
It works well within one scene and data are retrieved succesfully. But, when it switch from one scene to another scene, Error message "Unhandled exception at xxxxxxx in xxx.exe: Illegal Instruction" was prompted.

Also, the program runs without an error when I delete the line marked with * which is

Code: Select all

    name[i] = irr::core::string<irr::c8>(temp);
Can anybody help me with this? ... I have been working with it for days...

Thank you.
Last edited by winny8674 on Fri Feb 22, 2008 12:42 pm, edited 1 time in total.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

i think the problem is in priorities:
do not write:

Code: Select all

*name[i]
but write:

Code: Select all

(*name)[i]
OR

Code: Select all

*(name[i])
depends on what you really need.
winny8674
Posts: 4
Joined: Fri Feb 22, 2008 11:54 am

Post by winny8674 »

er... sorry , it is an indicator only... it is not originally included in the code

the indicatorof the line is now changed to ..... (*)
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

It could be a scope issue which your snippet of code is not showing.

Please post the entire code containing the scope of the symbol defined.
Image
winny8674
Posts: 4
Joined: Fri Feb 22, 2008 11:54 am

Post by winny8674 »

Don't know if it is a scope problem, but the socpe is a bit difficult to show here

I have difined a class to hold the engine device and the other class (class A) to handle all the things in the scene. Also I have built a class which is contained in class A to read file.

Code: Select all

class A{
...
private:
    B b;
}

Code: Select all

class B{
...
private:
    u16 n;
    u32* id;
    string* name;
    string* description;
...
}
The data read from a text file is put into a dynamic array. The program run normally if I remove the dynamic array and not loading those data. But it crashes if I add them in.

Code: Select all

constructor of class B
B::B(){
    std::ifstream in;
    in.open("data.dat");

    in >> n;

    id = new u32[n];
    name = new string[n];
    description = new description[n];

    for(int i = 0; i < n; i++){
        u32 k;
        char temp[256];

        in >> id[i];
        in.ignore();

        in.getline(temp, 255);
        name[i] = string(temp);
        in.getline(temp, 255);
        description[i] = string(temp);
    }

    in.close();
}

Code: Select all

destructor of class B
B::~B(){
    delete[] id;
    delete[] name;
    delete[] description;
}
VPB_Enge
Posts: 17
Joined: Wed Jan 02, 2008 11:35 pm

Post by VPB_Enge »

Not sure it is actually causing the problem, but in class B you define:
string* description;

In the constructor of class B, you use:
description=new description[n] -> this is bad.. same names

I wonder what the compiler does with that...

Try to rename the new string (pointer) array, for example:
description DescriptionArray=new description[n];

and at the deconstructor:
delete [] DescriptionArray;


Addition: Personally I would -never- use file reading during a game, if I could prefend it. The files you are reading are so small, that you can easily read them before the game starts... File IO eats cpu clicks.
On top of that it is good practice to actually CHECK for errors when you open a file.
FinalZero
Posts: 1
Joined: Sat Mar 01, 2008 3:04 pm

Post by FinalZero »

Thanks for advice and I am sorry about the typo
actually the correct one in class B should be

Code: Select all

B::B(){
...
description = new string[n];
...
}
However, the main problem we have encountered is the error Privileged instruction, Illegal Instruction, access violation etc. when the program change from one scene to and other.

I have tried to find out the problem but I still can't find it.
Actually the file reading part should be correct since the data is loaded correct and can be used in the game if I don't change scene. But when I change to another scene, the program encountered those exception. After I have removed those dynamic arrays and not loading those data, the program run without errors. I don't know why the program crash and it always crash after loading the scene data like this.

Code: Select all

A::A(IrrlichtDevice *_device){
    smgr = _device->getSceneManager();
    driver = _device->getVideoDriver();
    env = _device->getGUIEnvironment();

    smgr->loadScene("scene.irr");

    player = smgr->addAnimatedMeshSceneNode(smgr->getMesh("player.3ds"), 0, 1, vector3df(0,0,0), vector3df(0,0,0), vector3df(1,1,1), false);

...
}
And the exception cause the program break before adding the animated mesh.
Post Reply