passing TaskPtr to executor in scheduler to ensure when the
callback is envoked, the Task object is still there
diff --git a/scheduler/task.cc b/scheduler/task.cc
index 4702906..2f72f06 100644
--- a/scheduler/task.cc
+++ b/scheduler/task.cc
@@ -72,5 +72,12 @@
void
Task::execute()
{
- m_scheduler->execute(boost::bind(&Task::run, this));
+ // m_scheduler->execute(boost::bind(&Task::run, this));
+
+ // using a shared_ptr of this to ensure that when invoked from executor
+ // the task object still exists
+ // otherwise, it could be the case that the run() is to be executed, but before it
+ // could finish, the TaskPtr gets deleted from scheduler and the task object
+ // gets destroyed, causing crash
+ m_scheduler->execute(boost::bind(&Task::run, shared_from_this()));
}
diff --git a/scheduler/task.h b/scheduler/task.h
index 41e4438..11a9fa6 100644
--- a/scheduler/task.h
+++ b/scheduler/task.h
@@ -32,6 +32,7 @@
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
+#include <boost/enable_shared_from_this.hpp>
#include <sys/time.h>
//////////////////////////////////////////////////
@@ -49,7 +50,7 @@
/**
* @brief Base class for a task
*/
-class Task
+class Task : public boost::enable_shared_from_this<Task>
{
public:
// callback of this task