所以发生了一些奇怪的事情。我正在绑定回调函数(我在代码的其他部分中完成了几次),但是由于某种原因,它导致击曲线被调用,并在其上进行segfault…
这是我代码的大纲,所有多余的东西都剥离了
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();
}
GDB错误
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() {
不确定发生了什么。奇怪的是,删除其他构造函数参数(删除精灵和回调并评论所有相关代码)使我遇到了此错误。
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/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:1377
1377 if (__nd != nullptr)
这是我的util.h
中的Mousecode定义/**
* 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));
}
这是EventListEnerComponent在做什么
void EventListenerComponent::addMouseFunction(std::function<void(int, int)> func, Uint8 button, Uint8 state) {
Mousecode code = {button, state};
mouseFunctionMap[code] = func;
}
和MouseFunctionMap
std::map<Mousecode, std::function<void(int, int)>> mouseFunctionMap;
任何帮助将不胜感激……谢谢!
您正在创建一个临时副本通过*this
:
eventListener->addMouseFunction(std::bind(&GUILogicComponent::clicked, *this, std::placeholders::_1, std::placeholders::_2), SDL_BUTTON_LEFT);
这条线后立即被摧毁。
您没有显示其他副本/移动构造函数和操作员,我怀疑您没有编码它们。看看三个规则是什么?
这会导致同一指针的双重删除。
您应该使用std ::: unique_ptr,因为您的组件似乎拥有其所有权。