std :: bind导致析构函数

时间:2017-09-26 01:41:37

标签: c++ c++11 segmentation-fault

所以发生了一些奇怪的事情。我绑定了一个回调函数(我已经在代码的其他部分做了好几次)但是由于某种原因,这次它导致析构函数被调用,并且它上面有一个段错误...

这是我的代码的轮廓,其中删除了所有多余的东西

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;

非常感谢任何帮助......谢谢!

1 个答案:

答案 0 :(得分:1)

您正在创建一个通过*this的临时副本:

eventListener->addMouseFunction(std::bind(&GUILogicComponent::clicked, *this, std::placeholders::_1, std::placeholders::_2), SDL_BUTTON_LEFT);

这条线后立即被摧毁。

您没有展示其他复制/移动构造函数和运算符,我怀疑您还没有对它们进行编码。见What is The Rule of Three?
这导致同一指针的双重删除。

您应该使用std:::unique_ptr,因为您的组件似乎拥有自己的所有权。

相关问题