Jump to content

  • Log In with Google      Sign In   
  • Create Account


Why is this returning 0xfeefeee?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 johnmarinelli   Members   -  Reputation: 304

Like
0Likes
Like

Posted 17 February 2013 - 01:36 PM

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!


Edited by johnmarinelli, 17 February 2013 - 01:37 PM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9575

Like
4Likes
Like

Posted 17 February 2013 - 01:38 PM

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.

#3 Endurion   Crossbones+   -  Reputation: 3494

Like
0Likes
Like

Posted 17 February 2013 - 01:43 PM

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.


Edited by Endurion, 17 February 2013 - 01:44 PM.

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

#4 johnmarinelli   Members   -  Reputation: 304

Like
0Likes
Like

Posted 17 February 2013 - 01:46 PM

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.



#5 swiftcoder   Senior Moderators   -  Reputation: 9884

Like
0Likes
Like

Posted 17 February 2013 - 02:18 PM

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 - Software Engineer @Amazon - [swiftcoding]


#6 DrEvil   Members   -  Reputation: 1104

Like
0Likes
Like

Posted 17 February 2013 - 02:27 PM

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

#7 BGB   Crossbones+   -  Reputation: 1554

Like
0Likes
Like

Posted 17 February 2013 - 02:46 PM


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), ...

#8 Satharis   Members   -  Reputation: 949

Like
0Likes
Like

Posted 17 February 2013 - 02:46 PM

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.


Edited by Satharis, 17 February 2013 - 02:47 PM.


#9 swiftcoder   Senior Moderators   -  Reputation: 9884

Like
0Likes
Like

Posted 17 February 2013 - 04:11 PM

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 - Software Engineer @Amazon - [swiftcoding]


#10 L. Spiro   Crossbones+   -  Reputation: 13437

Like
3Likes
Like

Posted 17 February 2013 - 04:22 PM

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


Edited by L. Spiro, 17 February 2013 - 05:06 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#11 johnmarinelli   Members   -  Reputation: 304

Like
0Likes
Like

Posted 17 February 2013 - 07:06 PM

Hi all,

I did manage to fix the problem with your advice; however there are still some memory access errors that I'm getting, so I'll definently be restructuring my state machine.  Also,

 

Also, what is the purpose of this?

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

 

that was just a dumb error on my part rolleyes.gif You're completely right though.


Edited by johnmarinelli, 17 February 2013 - 07:07 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS