String Error

What does it mean if I am calling a function to read a string and it crashes with the debugger just pointing at the function? It doesn't go any farther. However if I put the same function call into throw runtime_error() it outputs the proper contents to the console?

 
string name = skillTypes[i]->getName();

This gives me an error where the VC++ debugger points to:
 
string getName() const {return m_name;}

In the class declaration. When I was fiddling I managed to get something where in xstring it told my something about an iterator. But I forget what I did.
 
throw runtime_error(skillTypes[i]->getName());

The above code causes the console to display the name I would expect to see. Why does it work for runtime_error but not assigning a string?

For reference I used another class that inherits from the one that getName() is from and the getName() called works fine. The constructor constructs the class with m_name set to "default".

I'm sorry if I am missing some info. If I knew any better what to show I could probably have solved it on my own.
Without seeing your code it is difficult to say what is the problem. I only can guess that it can be related with the index value used in expression skillTypes[i]
That can't be it as I explained. If it was some sort of index error then throwing a runtime_error wouldn't output the correct text. I call that using the same index in the same loop as the line that doesn't work. The first line is directly on top of the other one.

1
2
throw runtime_error(skillTypes[i]->getName());
string name = skillTypes[i]->getName();


That is the position of the two lines. The first one puts the proper text into the console. Then I comment it out and the second line throws the error.

1
2
3
4
5
6
7
    for (int i = 0; i < skillTypes.size(); ++i) {
        m = n->addChild("skill");
        //throw runtime_error(skillTypes[i]->getName());
        const SkillType *skill = skillTypes[i];
        string name = skill->getName();
        m->addAttribute("name", name);
    }


Maybe this will make it clearer. Uncommenting the throw line causes the console output to display the string I want. Otherwise I get the error.
Last edited on
It is not clear whether pointers stored in skillTypes are valid pointers.
If they weren't valid then how would runtime error output the expected string to the console? If they pointed to the wrong spot I should get some sort of access violation or something.
In my opinion the program has undefined behavior.
More like I have an undefined level of intelligence. Sometimes I work fine and sometimes I crash and burn.

If its any consolation you did help me. I probably wouldn't have figured it out if I wasn't so sure it wasn't my pointers. Your diagnosis was absolutely right but the problem was in such a different part of the program that I didn't check.

Of course it was the pointers. Turns out I had disabled initMoveSkill for this particular part of the program to focus on other stuff. It would work sometimes, probably if I got the error on the attack skill. It would always crash without the error because it would accept the first skill and crash on the non-existent second one or crash if that was the first one.
Topic archived. No new replies allowed.