Page 1 of 1
[SOLVED] return i = crash!? :S
Posted: Wed Dec 27, 2006 4:12 pm
by Xharock
Code: Select all
int cMenu::getElementNoFromName(std::string name)
{
int i;
for(i = 0; i < 20; i++)
{
if(m_elements[i]->getName() == name)
break;
else continue;
}
return i;
}
For some reason this function will crash on the return i line with this error:
Unhandled exception at 0x1048c1fe (msvcp80d.dll) in Metal Gear.exe: 0xC0000005: Access violation reading location 0xbbeaba4c.
And it says there is a problem in xstring:
Code: Select all
bool __CLR_OR_THIS_CALL _Grow(size_type _Newsize,
bool _Trim = false)
{ // ensure buffer is big enough, trim to size if _Trim is true
if (max_size() < _Newsize)
_String_base::_Xlen(); // result too long
if (_Myres < _Newsize)
_Copy(_Newsize, _Mysize); // reallocate to grow
else if (_Trim && _Newsize < _BUF_SIZE)
_Tidy(true, // copy and deallocate if trimming to small string
_Newsize < _Mysize ? _Newsize : _Mysize);
else if (_Newsize == 0)
_Eos(0); // new size is zero, just null terminate
return (0 < _Newsize); // return true only if more work to do
}
I have NO idea what's going on. If I replace return i with return 0 or any number it works fine. Any ideas :S
Posted: Wed Dec 27, 2006 4:31 pm
by CuteAlien
Most likely m_elements does contain an invalid pointer for some value of i. Also the "else continue" won't do anything, you can leave that out.
Posted: Wed Dec 27, 2006 4:36 pm
by Xharock
No, I've done debugging and m_elements[0] is definatly a valid pointer. If it wasn't surely it would not like the line where I compare the two strings? Anyway it is valid as this is an excerpt from a menu system I'm writing and it gets set through the menu file which has all worked up until now when i started to add some new features.
Posted: Wed Dec 27, 2006 4:46 pm
by CuteAlien
And how about m_elements[1] to m_elements[19]? It will crash if any of those is invalid.
Posted: Wed Dec 27, 2006 4:49 pm
by Xharock
It doesn't reach those in the tests I'm doing. The function will break from that loop as soon as it finds a match for the string provided and in the tests I'm doing m_elements[0] is that match.
Posted: Wed Dec 27, 2006 5:20 pm
by CuteAlien
Well, i don't know what m_elements is or what getName() does return, so there could be a bug which i can't see. But besides the strange "else continue;" and the possible problem that your m_elements objects could be invalid the function looks fine. And that else-line won't cause the crash.
You are writing something about xstring which is a class about which i don't know anything. I don't even see it used here.
Posted: Wed Dec 27, 2006 5:35 pm
by Xharock
getName() simply returns a value in the cMenuElements class which is what m_elements[] is. The else statement is just a remnant from a previous incarnation of the function so I just left it there as it's no cause of a problem. I'm not sure if xstring might be related to std::string in any way?
Posted: Wed Dec 27, 2006 5:53 pm
by CuteAlien
Try to rewrite the function. Assign the string to additional variable first. Print that variable. Compare after that with that variable.
I think you will find faster answers to such questions in a c++ irc-channel. This is not really irrlicht-related stuff.
Posted: Wed Dec 27, 2006 6:09 pm
by Xharock
OK It seems it doesn't work now if I replace return i with return 0 or any other number.
Posted: Wed Dec 27, 2006 6:12 pm
by CuteAlien
Most likely your bug is outside the function. The function itself looks fine.
Posted: Wed Dec 27, 2006 6:17 pm
by Xharock
I think you are right. I just replaced the function contents with this:
int x;
x = 0;
return x;
With the same crash. This is how I'm calling the function:
int elementNo;
elementNo = this->getElementNoFromName(onSelectName);
It is being called from within another function which belongs to the same class hence the use of this although it does work without using this->
Why is it that stupidly simple things like this crash on me??
Posted: Wed Dec 27, 2006 8:46 pm
by Xharock
OK Turns out I was debugging the wrong section of code. I've located the real trouble. Sorry about that
![Embarassed :oops:](./images/smilies/icon_redface.gif)