Sunday 15 April 2012

C++: Segmentation fault when seperating header and implementation file (.hpp/.cpp) -



C++: Segmentation fault when seperating header and implementation file (.hpp/.cpp) -

for simple game have class, checks if there objects in front end of game entity.

class fieldofview : public anax::system<fieldofview> { public: fieldofview() : base(anax::componentfilter().requires<components::transform, components::fieldofview>()) { } void update() { using std::find; using std::begin; using std::end; using std::remove_if; auto& bus = game::get().getmessagebus(); (auto& entity : getentities()) { auto collisions = getcollisions(entity); auto& insight = entity.getcomponent<components::fieldofview>().insight; (auto& collided : collisions) { if (find(begin(insight), end(insight), collided) == end(insight)) { insight.push_back(collided); bus.send<message::entityenteredfov>(entity, collided); } } std::function<bool(const anax::entity&)> hascollided = [&collisions](const anax::entity& x) -> bool { homecoming find(begin(collisions), end(collisions), x) != end(collisions); }; (const auto& seenentity : insight) { if ( !hascollided(seenentity) ) bus.send<message::entityleftfov>(entity, seenentity); } insight.erase(remove_if(begin(insight), end(insight), std::not1(hascollided)), end(insight)); } } };

this class located in file "fieldofview.hpp". works expected. however, want seperate header , implementation file, created "fieldofview.cpp".

void fieldofview::update() { // same code above }

i updated header file ("fieldofview.hpp"):

class fieldofview : public anax::system<fieldofview> { public: fieldofview() : base(anax::componentfilter().requires<components::transform, components::fieldofview>()) { } void update(); };

when code (hpp + cpp) run, programme aborts because of segmentation fault (segmentation fault: 11). error occurs in line:

bus.send<message::entityleftfov>(entity, seenentity);

when compile , run code using single hpp (no cpp file) fine, there no segmentation fault or other kind of error. have no thought causing behavior…

old, but: bus refer static fellow member of game? seems to. if case, liable "static initialisation order fiasco" whereby different .cpp files might instantiate own static variables, out of order compared other objects trying access them, perchance before they're ready.

source: happened me. had static const set of data, , static container of pointers data. defining these in different translation units led vague segfault, understand in principle won't waste time trying figure out exact mechanistic cause. so, moved offending instantiations single cpp file, set them in right order of dependency, , again.

c++ segmentation-fault

No comments:

Post a Comment