Why is this returning 0xfeefeee?

Started by
9 comments, last by johnmarinelli 11 years, 2 months ago

Hey guys,

So I'm using a finite state machine for AI in my game. Up until recently it has been working perfectly - however, yesterday, for no discernable reason, this piece of code says that the currentAIstate's vfptr is inaccessible right when the instance of c_Zombie spawns:


void c_Zombie::logic( Uint32 deltaTicks )
{
    currentAIstate->AIlogic(collisionBox, c_Entity_manager::getPlayer()->getBox(), xVel, yVel, up, down,  left, right, alerted);

/*some other code..*/
    if(condition){
        setnextAIstate(someState);
    }

    changeAIState(currentAIstate);
}



the code for switching AI states goes like this:


void changeAIState(c_AIstate* currentAIstate)
{
	if ( nextAIState != AI_NULL )
	{
		delete currentAIstate;

		switch (nextAIState)
		{
		case CHILL:
			currentAIstate = new c_Chillmode(); break;
		case GO_TO: 
			currentAIstate = new c_Gotomode(); break;
		case ATTACK:
			currentAIstate = new c_Attackmode(); break;
		case DEAD:
			currentAIstate = new c_Deadmode(); break;
		}
		AIstateID = nextAIState;
		nextAIState = AI_NULL;
	}
}

void setnextAIState( int state )
{
	if ( state != DEAD )
	{
		nextAIState = state;
	}
	else
		nextAIState = DEAD;
}

I can't figure out why it just decided to stop working recently;

also, I read that allocating memory during runtime is a bad idea - what other options do I have for changing AI states, assuming that the method I'm using right now is a bad idea?

here's a link to the github if you need more info: https://github.com/johnmarinelli/8bit-sh/tree/master/cpp

or, just leave a comment with whatever info you need; I've already had to delete one feature of the game because of this, and I don't want to have to do it again.

Thanks for your time!

Advertisement
0xfeeefeee is the debug fill pattern for freed memory. If you get an access violation involving that address it means you're probably accessing an object after it's been deleted or something similar.

As SiCrane said.

You maybe have a new value for nextAIState that is not covered by the switch/case statement. You might want to get into the habit of always providing a default case.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

0xfeeefeee is the debug fill pattern for freed memory. If you get an access violation involving that address it means you're probably accessing an object after it's been deleted or something similar.

I know; I'm wondering why it's doing that since in my changeAIstate function I assign the pointer to a new AIstate right away.

After some toying around, I've found out that the problem is a bit more 'tangled' than I thought, so I don't expect any solutions - however, my other question still stands:

If using heap memory at runtime is generally a bad idea, how are state machines implemented in games? I have three of them in my game (one for the gamestates, one for AI, and one for a certain little puzzle), and am wary about using them because of the reason above.

If using heap memory at runtime is generally a bad idea

There is nothing wrong with using heap memory per se. There is something very wrong with accessing an object after it has been deleted.

Creating and deleting state objects every time you change states is not a great idea for a number of reasons (one of them being the ease of introducing memory management bugs). Is there a reason why you don't just create all your state objects upfront and switch between them?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

You should probably just have your AI state pointer point to member states of the class rather than allocating new states and freeing. As someone mentioned from the code provides so far it seems likely that you have a state not handled by the switch.

Enabling this warning may help


C4061 (level 4)
enumerator 'identifier' in a switch of enum 'enumeration' is not explicitly handled by a case label

One of many useful warnings disabled by default

http://msdn.microsoft.com/en-us/library/23k5d385.aspx


0xfeeefeee is the debug fill pattern for freed memory. If you get an access violation involving that address it means you're probably accessing an object after it's been deleted or something similar.


I know; I'm wondering why it's doing that since in my changeAIstate function I assign the pointer to a new AIstate right away.

After some toying around, I've found out that the problem is a bit more 'tangled' than I thought, so I don't expect any solutions - however, my other question still stands:
If using heap memory at runtime is generally a bad idea, how are state machines implemented in games? I have three of them in my game (one for the gamestates, one for AI, and one for a certain little puzzle), and am wary about using them because of the reason above.


most likely, at least given what is shown, there is a fall-through in the switch (some unexpected value here, ...).
try adding a 'default' case, to "do something useful" here.

maybe also try setting the pointer to NULL after deleting it, if anything, to help prevent accidentally reusing it.


otherwise:
much more common (AFAIK) for state machines is either using numerical state-values (and lots of switch blocks), or using function or method pointers to represent states (so, every time the state changes, the appropriate method-pointer is assigned).

other possible strategies include not heap allocating the objects, or allocating them once and simply reusing the instances (rather than using endless new/delete pairs), ...

For AI you don't necessarily need to create and delete a new state each time, as the others have said. I actually have written a few game state managers that create new states and swap them, as well as unload the old ones on change. Generally the way I handled that was by setting a flag and a temporary variable that holds a reference to the newly created gamestate, then have the gamestate physically swap when code isn't actually running through it in order to avoid access violations and such using the parent GameStateManager class.

Of course there are a lot of ways to design things like this.

Actually, looking at your code again, I'm pretty sure that your changeAIState() function doesn't actually do anything.

The switch statement is assigning the new state objects to currentAIstate, which is a local variable. You either need to return the created state object, or pass currentAIstate as a reference-to-pointer.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Indeed. It should have been this:


void changeAIState(c_AIstate* & currentAIstate)

Also, what is the purpose of this?


void setnextAIState( int state )
{
	if ( state != DEAD )
	{
		nextAIState = state;
	}
	else
		nextAIState = DEAD;
}

It is exactly the same thing as:


void setnextAIState( int state )
{
	nextAIState = state;
}

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement