String allocation problem?

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
_Neo
Posts: 30
Joined: Thu May 24, 2012 7:47 pm
Location: SiChuan, China

String allocation problem?

Post by _Neo »

Hey there, I'm just trying to read information from xml file and store them in the path(a data type in irrlicht).
Recently I found that if I press F5 in vs2008(run game in debug mode), every thing works right. However, when I run game directly(ctrl+F5) without debug mode, it crashes.
It turns out the string is empty after loading the imformation from xml..
here's the code:

Code: Select all

 
void tankFactory::build()
{
    path path_body,path_turret,path_cannon,path_trackModelL,path_trackModelR;
       //a lot of path type here
        IFileSystem*fs=device->getFileSystem();
    IXMLReader*xml=fs->createXMLReader(stringc("models/vehicle/ground/")+tType+"/modelInfo.xml");
 
    while(xml&&xml->read())
    {
        switch(xml->getNodeType())
        {
        case EXN_ELEMENT:
            stringc nodeName=xml->getNodeName();
            if (nodeName.equals_ignore_case(L"attributes"))
            {
                IAttributes *attr=fs->createEmptyAttributes();
                attr->read(xml,true);
                path_body=attr->getAttributeAsString("body");
                path_trackModelL=attr->getAttributeAsString("leftTrack");
                path_trackModelR=attr->getAttributeAsString("rightTrack");
//same operation with other string
                       }
          }
//load the model with path loaded above
    mesh_body=smgr->getMesh(stringc("./models/vehicle/ground/")+tType+"/"+path_body);
    mesh_trackL=smgr->getMesh(stringc("./models/vehicle/ground/")+tType+"/"+path_trackModelL);
    mesh_trackR=smgr->getMesh(stringc("./models/vehicle/ground/")+tType+"/"+path_trackModelR);
    mesh_turret=smgr->getMesh(stringc("./models/vehicle/ground/")+tType+"/"+path_turret);
//similiar operation
//.......other code
}
 
And it turns out right when running in the debug mode(F5),
but when running directly(ctrl+F5,or open the .exe file in windows), the one of the path value would be empty after the information is loaded.
when game crashes, the path object's string array is empty while the variable "allocated" and "used" in the object is not 0.
(e.g path_body , the xml wirtes:"body.x";
by the time when I trying to load mesh,
it becomes: array="",allocated=6,used=6, the array is missing)

I heard that visual studio would automatically initialize the variable when debuging, but if you run your program in windows, you should initialize ot yourself..
So..What should I do?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: String allocation problem?

Post by CuteAlien »

Hm, not fully getting it. Seems it loads some wrong string or so. Maybe you can add more log-information so you see exactly in which step it messes up your string.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
_Neo
Posts: 30
Joined: Thu May 24, 2012 7:47 pm
Location: SiChuan, China

Re: String allocation problem?

Post by _Neo »

Well....I've added a std::cout after the reading line like

Code: Select all

path_trackModelR=attr->getAttributeAsString("rightTrack");
to see if it successfully read the data.
The result is, it did read the right data, but when program run to the line:

Code: Select all

mesh_trackR=smgr->getMesh(stringc("./models/vehicle/ground/")+tType+"/"+path_trackModelR);
the variablve path_trackModelR loses its array..Thus the mesh_trackR turns out empty. Then the program crashes by the time I want to do something with mesh_trackR. :(

BTW, you see this function is a member of a class, this bug occurs everytime when I call this function the second time..
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: String allocation problem?

Post by hybrid »

Which probably means that you overwrite the array in the first call. I guess it's safe to assume that the irrString classes work as expected for now. However, you also wrote above that using the fixed char array did not work. So which versions did you test?
Abraxas)
Posts: 227
Joined: Sun Oct 18, 2009 7:24 am

Re: String allocation problem?

Post by Abraxas) »

I haven't used the path stype strings, but when you do:

+path_trackModelR

is it returning the value of the string, or a pointer to it's position? do you need something like path_trackModelR.c_str()? or (stringw) path_trackModelR?
_Neo
Posts: 30
Joined: Thu May 24, 2012 7:47 pm
Location: SiChuan, China

Re: String allocation problem?

Post by _Neo »

hybrid wrote:Which probably means that you overwrite the array in the first call. I guess it's safe to assume that the irrString classes work as expected for now. However, you also wrote above that using the fixed char array did not work. So which versions did you test?
Previously, I use 1.7.1, and now I've changed to 1.8, program still crash...
I am confused because in vs2008, if I test it by press F5(run it via vs2008), everything is ok.
But if I try to run my game directly in windows,this bug will occur...
_Neo
Posts: 30
Joined: Thu May 24, 2012 7:47 pm
Location: SiChuan, China

Re: String allocation problem?

Post by _Neo »

Abraxas) wrote:I haven't used the path stype strings, but when you do:

+path_trackModelR

is it returning the value of the string, or a pointer to it's position? do you need something like path_trackModelR.c_str()? or (stringw) path_trackModelR?
I've read the source, path type is the same as stringc...
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: String allocation problem?

Post by hybrid »

No, path type depends on the UNICODE setting for your filesystem choice. By default, it is stringc, though. I'd say the nullpointer happens due to a file not being loadable, and hence passing an empty mesh or so. Could also be a null char array somewhere, which is != 0 if not cleared by the debugger. We can only say that by seeing the full code, though.
Abraxas)
Posts: 227
Joined: Sun Oct 18, 2009 7:24 am

Re: String allocation problem?

Post by Abraxas) »

Just a question,

could there be an issue that your switch statement doesn't have a break?
_Neo
Posts: 30
Joined: Thu May 24, 2012 7:47 pm
Location: SiChuan, China

Re: String allocation problem?

Post by _Neo »

Abraxas) wrote:Just a question,

could there be an issue that your switch statement doesn't have a break?
Er..I've checked it...Every case has a break.
Abraxas)
Posts: 227
Joined: Sun Oct 18, 2009 7:24 am

Re: String allocation problem?

Post by Abraxas) »

likely the problem concerns parts of your code that you didn't post.
Post Reply