2

I am trying to register the Multiple Functions for callback using std::function and trying them to get called back whenever some event occurs.

class DemoClass
{
    public:
        DemoClass();

        void Callback1(int x);
        void Callback2(int x);
        void callEventPrinter(); //Just for Demo purpose
        private:
            int xVar;
            EventHandler *handler;
};

DemoClass::DemoClass():xVar(), handler(nullptr)
{
    CallBackRegister *handler = new EventHandler();
    using namespace std::placeholders;
    std::shared_ptr<std::function<void(int)>> func1 = std::make_shared<std::function<void(int)>>(std::bind(&DemoClass::Callback1, this, _1));
    handler->addHandler(func1, 7);
}

void DemoClass::callEventPrinter()
{
    handler->printOnCallBacks();
}

void DemoClass::Callback1(int x)
{
    cout << "Callback1: " << x + xVar << endl;
}

void DemoClass::Callback2(int x)
{
    cout << "Callback2: " << x + xVar << endl;
}

Here is the class where I am trying to register muliple callbacks and storing them in Map for future use.

class CallBackRegister
{
    public:
        void addHandler(std::shared_ptr<std::function<void(int)>> callback, int id)
        {
            cout << "Calback Handle added..." << endl;
            callBackSet[id] = std::move(callback);
        }

        void printOnCallBacks(){
            std::shared_ptr<std::function<void(int)>> callback = callBackSet[7]; //HERE SEGMENTATION FAULT IS GENERATED
        }
        CallBackRegister():callBackSet(){};
    private:
        std::map<int, std::shared_ptr<std::function<void(int)>>>callBackSet;
};

I am generating Segmentation Fault in printOnCallBacks() method, comment mentioned in line.

Giving main just for reference.

int main(){
    DemoClass *demoClass = new DemoClass();
    demoClass->callEventPrinter();
    return 0;
}

Reference Question followed : C++ class member callback simple examples

seccpur
  • 4,996
  • 2
  • 13
  • 21
spt025
  • 2,134
  • 2
  • 20
  • 26
  • 2
    You calling method on `nullptr` which is UB – Slava Jun 04 '18 at 13:59
  • Compiler flags used : g++ -std=c++0x -o – spt025 Jun 04 '18 at 13:59
  • @Slava Can you explain a little bit please ?, Because nullcheck also is not failing – spt025 Jun 04 '18 at 14:00
  • 5
    You create local pointer `handler` in `DemoClass::DemoClass()` (and shadow `this->handler` by that) and assign value to it, while `this->handler` is initialized by `nullptr` and keeps that value. Later you call method on `this->handler` which is `nullptr` – Slava Jun 04 '18 at 14:01
  • 2
    Generally, why do you need shared_ptr on std::function? Looks like unnecessary compilcated code. – Alex F Jun 04 '18 at 14:03
  • Agreed, can be doen without shared_ptr , I think @Slava is correct. It was very silly mistake which I spent almost 4 hours on... – spt025 Jun 04 '18 at 14:06
  • a debugger would have saved you these 4 hours ;) – Tom Mekken Jun 04 '18 at 14:07
  • Yes, it would have :( :( :( , I felt I was using std::function incorrectly, first time for that.. Started with heap / stack went till full readup of reference doc. Thanks for responses – spt025 Jun 04 '18 at 14:09
  • @Slava Thanks , it is correct solution... – spt025 Jun 04 '18 at 14:09

0 Answers0