So something strange is happening. I'm binding a callback function (which I've done several times in other parts of the code) but for some reason this time it's causing the destructor to be called, and a segfault on it…
Here's the outline of my code with all the superfluous stuff stripped
GUILogicComponent.h
class GUILogicComponent : public LogicComponent {
private:
std::function<void()> callback;
EventListenerComponent* eventListener;
SpriteComponent& sprite;
public:
GUILogicComponent(EventListenerComponent* el, SpriteComponent& s, std::function<void()> cb);
~GUILogicComponent();
void clicked(int x, int y);
};
GUILogicComponent.cpp
GUILogicComponent::GUILogicComponent(EventListenerComponent* el, SpriteComponent& s, std::function<void()> cb) : eventListener(el), sprite(s), callback(cb) {
eventListener->addMouseFunction(std::bind(&GUILogicComponent::clicked, *this, std::placeholders::_1, std::placeholders::_2), SDL_BUTTON_LEFT);
// TODO: Binding causes seg fault
}
GUILogicComponent::~GUILogicComponent() {
delete eventListener;
}
void GUILogicComponent::clicked(int x, int y) {
if (sprite.pointInSprite(x, y))
callback();
}
The GDB error
Thread 3 received signal SIGSEGV, Segmentation fault.
0x0000000100004e73 in Thor_Lucas_Development::GUILogicComponent::~GUILogicComponent (
this=<error reading variable: Cannot access memory at address 0x7fff5f3ffff8>)
at Components/GUILogicComponent.cpp:11
11 GUILogicComponent::~GUILogicComponent() {
Not sure what's going on. Strangely enough, removing the other constructor parameters (removing sprite and callback and commenting out all the relevant code) leaves me with this error instead.
Thread 3 received signal SIGSEGV, Segmentation fault.
0x00000001000027b8 in std::__1::__tree<std::__1::__value_type<Thor_Lucas_Development::Mousecode, std::__1::function<void (int, int)> >, std::__1::__map_value_compare<Thor_Lucas_Development::Mousecode, std::__1::__value_type<Thor_Lucas_Development::Mousecode, std::__1::function<void (int, int)> >, std::__1::less<Thor_Lucas_Development::Mousecode>, true>, std::__1::allocator<std::__1::__value_type<Thor_Lucas_Development::Mousecode, std::__1::function<void (int, int)> > > >::destroy(std::__1::__tree_node<std::__1::__value_type<Thor_Lucas_Development::Mousecode, std::__1::function<void (int, int)> >, void*>*) (this=0x10070c768, __nd=0x1007365d0)
at /Applications/http://ift.tt/2y4z9fD
1377 if (__nd != nullptr)
Here's the Mousecode definitions in my Util.h
/**
* Used to store a mouse state for mapping to functions.
*/
struct Mousecode {
Uint8 button; /**< The mouse button pressed (i.e\. SDL_BUTTON_LEFT). */
Uint8 state; /**< The state of the mouse button (i.e\. SDL_RELEASED). */
};
inline bool const operator==(const Mousecode& l, const Mousecode& r) {
return (l.button == r.button) && (l.state == r.state);
}
inline bool const operator<(const Mousecode& l, const Mousecode& r) {
return (l.button < r.button) || ((l.button == r.button) && (l.state < r.state));
}
Here's what EventListenerComponent is doing
void EventListenerComponent::addMouseFunction(std::function<void(int, int)> func, Uint8 button, Uint8 state) {
Mousecode code = {button, state};
mouseFunctionMap[code] = func;
}
And mouseFunctionMap
std::map<Mousecode, std::function<void(int, int)>> mouseFunctionMap;
Any help will be greatly appreciated… Thank you!
Aucun commentaire:
Enregistrer un commentaire