C++ std::bind导致析构函数上出现分段错误

C++ std::bind导致析构函数上出现分段错误,c++,c++11,segmentation-fault,C++,C++11,Segmentation Fault,所以发生了一些奇怪的事情。我绑定了一个回调函数(我在代码的其他部分已经绑定了好几次),但是由于某种原因,这次它导致调用析构函数,并且在它上面有一个segfault 下面是我的代码大纲,去掉了所有多余的东西 GUILogicComponent.h class GUILogicComponent : public LogicComponent { private: std::function<void()> callback; EventListenerComponent

所以发生了一些奇怪的事情。我绑定了一个回调函数(我在代码的其他部分已经绑定了好几次),但是由于某种原因,这次它导致调用析构函数,并且在它上面有一个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:公共逻辑组件{ 私人: 函数回调; EventListenerComponent*eventListener; SpriteComponent&sprite; 公众: GUILogicComponent(EventListenerComponent*el、SpriteComponent&s、std::function cb); ~GUILogicComponent(); 单击无效(整数x,整数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();
}
GUILogicComponent::GUILogicComponent(EventListenerComponent*el、SpriteComponent&s、std::函数cb):eventListener(el)、sprite(s)、回调(cb){ eventListener->addMouseFunction(std::bind(&GUILogicComponent::clicked,*this,std::placeholders::\u 1,std::placeholders::\u 2),SDL\u按钮\u左); //TODO:绑定导致seg故障 } GUILogicComponent::~GUILogicComponent(){ 删除eventListener; } void GUILogicComponent::单击(整数x,整数y){ if(sprite.pointInSprite(x,y)) 回调(); } 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() {
线程3收到信号SIGSEGV,分段故障。
Thor_Lucas_开发中的0x0000000100004e73::GUILogicComponent::~GUILogicComponent(
这个=)
at组件/GUILogicComponent.cpp:11
11 GUILogicComponent::~GUILogicComponent(){
不确定发生了什么。奇怪的是,删除其他构造函数参数(删除sprite和callback并注释掉所有相关代码)会让我产生这个错误

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)
线程3收到信号SIGSEGV,分段故障。
std::_1::_树::销毁(std::_1::_树节点*)中的0x00000001000027b8(此节点=0x10070c768,_nd=0x1007365d0)
在/Applications/Xcode.app/Contents/Developer/toolschains/XcodeDefault.xctoolschain/usr/bin//include/c++/v1/__树:1377
1377如果(\uu nd!=nullptr)
下面是我的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));
}
/**
*用于存储映射到函数的鼠标状态。
*/
结构鼠标码{
Uint8按钮;/**<按下鼠标按钮(即左SDL按钮)*/
Uint8状态;/**<鼠标按钮的状态(即SDL_已释放)*/
};
内联布尔常量运算符==(常量鼠标编码&l,常量鼠标编码&r){
返回(l.button==r.button)&(l.state==r.state);
}

inline bool const运算符您正在创建一个临时副本,并在此处传递
*此

eventListener->addMouseFunction(std::bind(&GUILogicComponent::clicked, *this, std::placeholders::_1, std::placeholders::_2), SDL_BUTTON_LEFT);
在这条线之后马上就被摧毁了

您没有显示其他复制/移动构造函数和运算符,我怀疑您没有对它们进行编码。请参见
这会导致重复删除同一指针


你应该使用你的组件拥有它的所有权。当它被添加到C++ 11时,

<代码> BION/COD>大部分过时了。我看这里没有任何东西可以用lambda做得更清楚。考虑一下这样做;更少的魔法,更清楚的是什么是拷贝和什么是引用。尝试<代码> STD:::绑定。(&GUILogicComponent::单击,此,…),在代码之前没有一个星体>这个< /代码>。我怀疑你正在制作一个<代码> GeLogic组件< /> >,并把它传递给<代码> BION/COD>。谢谢!@ IgorTandetnikWow,我感觉非常愚蠢!谢谢你,我刚刚删除了*,不去解释这个。边注:我应该考虑使用SydDypTR来为我的实体吗?所有权,但有时单个组件需要相互了解。我的做法是在析构函数中传递指向要删除的实体的指针*,但传递对其他组件的引用。@ThorCorreia如果您的组件共享该所有权,则可以使用共享的\u ptr。
std::map<Mousecode, std::function<void(int, int)>> mouseFunctionMap;
eventListener->addMouseFunction(std::bind(&GUILogicComponent::clicked, *this, std::placeholders::_1, std::placeholders::_2), SDL_BUTTON_LEFT);