16417

Cannot figure out Seg Fault, lots of detail provided

Question:

I asked this question about a half hour ago, but the code had typos and I wasn't being very clear, so I've deleted and I'm trying again with a better format.

I'm getting a Segmentation Fault in my code, the problem seems to be at the function call if( (*trans).test((*this), *(*itr)) ) inside World::update():

void World::update(sf::Time dt) { mPlayer->setVelocity(0.f, 0.f); while (!mCommandQueue.isEmpty()){ Command cmd = mCommandQueue.pop(); cmd.action(cmd.node, dt); } mSceneGraph.update(dt); adaptPlayerPosition(); //Enemy Spawn engine if (mSpawnTimer.getElapsedTime().asSeconds() >= SPAWN_INTERVAL && mEnemies.size() < MAX_NUM_ENEMIES){ float winX = mWindow.getDefaultView().getSize().x; float winY = mWindow.getDefaultView().getSize().y; float x = rand() % (int)winX; float y = rand() % (int)winY; spawnEnemy(x, y, EnemyType::Loner, IState::ILWander); mSpawnTimer.restart(); } // FSM update IState::ID curEnemyStateID; FState curEnemyState; bool trigger = false; ICondition triggeredtrans; FState triggeredState; for(auto itr = mEnemies.begin(); itr != mEnemies.end(); ++itr){ curEnemyStateID = (*itr)->getState(); // set curState to whatever the enemy's curState is switch(curEnemyStateID){ case 0: curEnemyState = LWander; break; default: break; } auto tState = curEnemyState.getTransitionStates().begin(); for(auto trans = curEnemyState.getConditions().begin(); trans != curEnemyState.getConditions().end(); ++trans){ if( (*trans).test((*this), *(*itr)) ){ trigger = true; triggeredState = (*tState); break; } ++tState; } if(trigger){ (*itr)->setState(IState::ILRushPlayer); curEnemyState = LRushPlayer; } curEnemyState.getAction()->doAction((*this), *(*itr)); } } <hr />

Context:

trans is an iterator for a std::vector<ICondition> conditions where each ICondition has a test(World& world, Enemy& enemy). itr is an iterator through a std::vector<Enemy*> that is held by World.

The conditions vector is filled in this function:

void World::initializeStates() { Wander LWanderAction; LWander.setAction(LWanderAction); DistFromPlayer LWanderCond1(30); LWander.pushCondition(LWanderCond1); LWander.pushTransitionState(LRushPlayer); }

LWander is a state (FState) in my Finite State Machine. Wander is a class that inherits IAction, and setAction accepts an IAction parameter: FState::setAction(IAction iact)

DistFromPlayer is a class that inherits ICondition.

FState::pushCondition(ICondition icond) and FState::pushTransitionState(Fstate state) should take their arguments and push them to Fstate's conditions and states vectors. (A transition's condition and matching target state should be at the same indices in both)

LWander and LRushPlayer are both members of World.

<hr />

And that should cover everything. I don't know why I'm getting a SegFault, but I'm assuming that the problem is with how things are pushed into LWander in World::initializeStates(). I should also note that the SegFault occurs right after the first enemy is spawned in my game, which is also the same update frame that runs (*trans).test(*this, **itr) for the first time. All Enemys start in the LWander state.

<hr />

ICondition's virtual bool test(World& world, Enemy& enemy); is defined as:

bool ICondition::test(World& world, Enemy& enemy){ return false; //returns false by default, overwritten later }

and DistFromPlayer's bool test(World& world, Enemy& enemy); is defined as:

bool DistFromPlayer::test(World& world, Enemy& enemy) { std::cout << "DistFromPlayer (LWander's Transition) was reached\n"; return false; }

and only contains a print statement for debugging purposes.

<hr />

GDB's backtrace

#0 World::update (this=0x6464408, dt=...) at C:\...\World.cpp:97 #1 0x0040416b in GameState::update (this=0x64643f0, dt=...) at C:\...\GameState.cpp:22 #2 0x00402435 in StateStack::update (this=0x28fde0, dt=...) at C:\...\StateStack.cpp:19 #3 0x00403782 in Game::update (this=0x28fbc0, elapsedTime=...) at C:\...\Game.cpp:58 #4 0x004036a2 in Game::run (this=0x28fbc0) at C:\...\Game.cpp:48 #5 0x0040888b in main () at C:\...\main.cpp:7

Answer1:

I am suspecting tState iterator. You only initialize it with begin() and increment it. I can't find any test against end() of the appropriate container. Or is it safe because of some relation between curEnemyState.getTransitionStates() and curEnemyState.getConditions() which I don't realize?

Recommend

  • Child div height same as sibling div Bootstrap 3?
  • React Bootstrap Checkbox in Nav -> NavItem does not rerender on click
  • Deleting a widget from QTableView
  • Avoid registering duplicate broadcast receivers in Android
  • Deploying a CodeRush plugin from the Community Site
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Question about instantiating object
  • Validate child input components on submit with Vee-Validate and vue js 2
  • Build Successful but not running on simulator
  • Trouble saving JSON data for use by multiple UIViews [duplicate]
  • saving file generated by TCPDF
  • Force show.bind execution
  • wxPython: displaying multiple widgets in same frame
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • how to save the state in userdefaults of accessory checkmark-iphone
  • Chart.js Multiple dataset
  • Groovy: Unexpected token “:”
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • How to write order and limit within cakephp joins array
  • Custom validator control occupying space even though display set to dynamic
  • Word Open XML Mail Merge
  • Android fill_parent issue
  • FileReader+canvas image loading problem
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Pass value from viewmodel to script in zk
  • Using $this when not in object context
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • Counter field in MS Access, how to generate?
  • Sony Xperia Z Tablet not found by adb
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?