• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
johnmarinelli

Why is this returning 0xfeefeee?

10 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites


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

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

 

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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0