Advertisement

Program crashing

Started by December 16, 2003 04:34 AM
22 comments, last by Ruudje 21 years, 2 months ago
Hey all. Im (still) working on a big 3d online project, but Ive run into some trouble. Each time I want to load a new world, I call a function to do so. This function reads in scripts from ini-files, and loads 3ds models. It then generates displaylists of those models. The problem is, the function only works once. during startup, I call the function, and the initial world is loaded just fine. If I call it again later to laod a new one however, the entire program freezes. Can anyone tell me why?

void TMainform::LoadWorldFromIni(AnsiString FileName)
{
        /*

                Load in the main world/map/scene or whatever

        */
        AnsiString CorrectedFileName = ".\\data\\" + FileName;

        TIniFile *ini;

        if(FileExists(FileName))
                ini = new TIniFile( FileName );
        else
                ini = new TIniFile( CorrectedFileName );

        // We need the following
        // - world id
        // - number of objects in it
        // - world mesh
        // - the id''s of the objects in the world

        // Read the ID
        world_id = ini->ReadInteger( "Wereld", "ID", 1 );

        // Number of objects
        num_objects = ini->ReadInteger( "Wereld", "Aantal", 0 );
        
        // World mesh
        AnsiString world_mesh = ini->ReadString( "Wereld", "Wereld", "");

        // Player startposition
        AnsiString player_start = ini->ReadString("Wereld", "Start", "0,0,0");

        char *x, *y, *z;

        x = strtok(player_start.c_str(), ",");
        y = strtok(NULL, ",");
        z = strtok(NULL, ",");

        Player[PlayerID].Position.x = StrToFloat(x);
        Player[PlayerID].Position.y = StrToFloat(y);
        Player[PlayerID].Position.z = StrToFloat(z);

        // If there is an old mesh present, delete it first
        if(World_model)
        {
                delete World_model;
                World_model = 0;
        }

        // Load the model
        if(FileExists(world_mesh))
        {
                World_model = new CModel;
                World_model->Load(world_mesh.c_str() );

                WorldDisplaylist = glGenLists(1);
                
                glNewList(WorldDisplaylist, GL_COMPILE);
                World_model->Render();
                glEndList();
        }

        // Load all objectscripts
        for(int i = 1; i <= num_objects; i++)
        {
                AnsiString scriptline = ini->ReadString( "Wereld", "Object"+IntToStr(i), 0 );

                // Pointers to substrings. The id is at 0
                char *x, *y, *z, *xrot, *yrot, *zrot, *targetworld, *url;

                // Decipher the scriptline
                strtok(scriptline.c_str(), ",");
                x = strtok(NULL, ",");
                y = strtok(NULL, ",");
                z = strtok(NULL, ",");
                xrot = strtok(NULL, ",");
                yrot = strtok(NULL, ",");
                zrot = strtok(NULL, ",");
                targetworld = strtok(NULL, ",");
                url = strtok(NULL, ",");

                // Now convert these substrings into useable values
                worldobject[i-1].id = StrToInt(scriptline) - 1;
                worldobject[i-1].Position.x = StrToInt(x);
                worldobject[i-1].Position.y = StrToInt(y);
                worldobject[i-1].Position.z = StrToInt(z);
                worldobject[i-1].Rotation.x = StrToInt(xrot);
                worldobject[i-1].Rotation.y = StrToInt(yrot);
                worldobject[i-1].Rotation.z = StrToInt(zrot);

                worldobject[i-1].Targetworld = String(targetworld);

                if(worldobject[i-1].Targetworld == "0")
                        worldobject[i-1].Targetworld = "";

                worldobject[i-1].Url = String(url);

                if(worldobject[i-1].Url == "0")
                        worldobject[i-1].Url = "";
        }

        delete ini;	
}
 
Is it possible that it is caused by me trying to create new a displaylist of the world, even though the old one still exists? any help would be greatly appreciated
Where does your program crash?

--------------------------------------------------------

"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"

Corrail
corrail@gmx.at
ICQ#59184081
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Advertisement
The ide doesnt show it. The program just freezes as soon as I call that function. (the ide doesnt highlight any lines)
Are you sure the ini file is complete? The rest seems ok to me.
Try loading the same world twice, look if it crashes then, if not, the 2nd ini file you tried to load isn't complete.

Or:

Try to set breakpoints.

[edited by - Living Monstrosity on December 16, 2003 6:37:47 AM]
- growl -
I already tried that it loads the same ini the second time
Is AnsiString a self-written class? Maybe there are some memory problems?

--------------------------------------------------------

"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"

Corrail
corrail@gmx.at
ICQ#59184081
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Advertisement
OR you can try not deleting everything you new.
I got access violation when deleting (or freeing when using malloc) is some of my functions for no clear reason at all, so i simply commented all delete blabla lines and now it workt fine, the memory is even deleted automatically when the pointer''s scope expires.
- growl -
Thats a good idea. ill try that.

and the AnsiString is a standard class for strings from CBuilder
quote:
Original post by Living Monstrosity
OR you can try not deleting everything you new.
I got access violation when deleting (or freeing when using malloc) is some of my functions for no clear reason at all, so i simply commented all delete blabla lines and now it workt fine, the memory is even deleted automatically when the pointer''s scope expires.


This is extremely bad! The memory is not deleted automatically in C or C++. There is an error in your code if the deletes give you access violations(i.e. your deleting memory that wasn''t allocated or double deleting it). The only time the memory MAY be freed is when the program closes completely. In newer systems this happens i.e. XP/2000 but older OS''s 9x don''t do this so you will be losing some memory until you reboot. You should try to find out why it is crashing instead - you ARE doing something wrong!

James
well, with this code:
if(p){  free(p);};// OR when using newif(p){  delete p;};

i get access violation, so does that mean that the array is allocated, but not really there?
- growl -

This topic is closed to new replies.

Advertisement