[SOLVED] return i = crash!? :S

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
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

[SOLVED] return i = crash!? :S

Post 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
Last edited by Xharock on Wed Dec 27, 2006 9:04 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9693
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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.
CuteAlien
Admin
Posts: 9693
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

And how about m_elements[1] to m_elements[19]? It will crash if any of those is invalid.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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.
CuteAlien
Admin
Posts: 9693
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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?
CuteAlien
Admin
Posts: 9693
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post by Xharock »

OK It seems it doesn't work now if I replace return i with return 0 or any other number.
CuteAlien
Admin
Posts: 9693
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Most likely your bug is outside the function. The function itself looks fine.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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??
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post by Xharock »

OK Turns out I was debugging the wrong section of code. I've located the real trouble. Sorry about that :oops:
Post Reply