Function callback using std :: bind causes a segmentation error

I have a class (let it be called "Common") that is used to create timers and call a member function from another class (let it be called "A") when the timer expires. The general idea is: - Calls a function from Common, passing it a pointer to a member function that should be executed when the timer expires - Common stores a pointer to a function on the card with the timer identifier key - When the timer enters, the card looks for the corresponding key, and then the corresponding key is called function.

Here is the code for A:

void A::setTimer()
{ 
   std::function<void(void)> handler = std::bind(&A::onEventFunc, this);
   pCommon->scheduleTimer(&handler);
}

onEventFunc is a public function of class A that does something when the timer expires.

Here is the code for Common:

static void timerHandler(int sig, siginfo_t *si, void uc)
{
   map<timer_t*, std::function<void>(void)>*)::iterator iter = 
                                globalTimerMap.find((timer_t*)si->si_value.sival_ptr);
   if (iter != globalTimerMap.end)
   {
      std::function<void>(void)>* handler = iter->second;
      (*handler)();
   }
}

Common::schedulerTimer(std::function<void>(void)* handler)
{
   // uses Linux timer_create and timer_settime
   // code for these excluded EXCEPT the relevant lines
   sa.sa_sigaction = timerHandler;

   // bunch of other code here for timer_create and timer_settime

   // insert into map
   globalTimerMap.insert(pair(&timerid, handler);
}

, (* handler)(); , segmantation.

, Linux , ++, . , - /, , . , , ?

+3
1

:

std::function<void(void)> handler = std::bind(&A::onEventFunc, this);

( - setTimer). , .

, std::function, . , , :

std::function<void(void)>* handler = new std::function<void()>(std::bind(&A::onEventFunc, this));

delete , , std::unique_ptr .

+5

All Articles