Search code examples
c++boostdllshared-ptr

Segmentation fault while handling shared_ptr


TaskManager.h

# ifndef TASKMANAGER_H
# define TASKMANAGER_H

# include "Scheduler.h"
# include <thread>
# include <mutex>
# include <condition_variable>
# include <vector>
# include <iostream>
# include <boost/asio.hpp>
# include <boost/make_shared.hpp>
# include <vector>

using namespace std;
using namespace boost::asio;


class Task_Manager{

private:
    static int i ;

    vector <pair <const boost::shared_ptr<ITask>& ,string > >  ptr_list;  

public:
    Task_Manager();
    ~Task_Manager();
    ITask* create_task(string polling_frequency,string sName,int);
    void register_task(string sName,boost::shared_ptr<Scheduler> scheduler_ptr,string polling_frequency,int );
    void get_data_from_remote_icmp();
private:
    void register_task_to_scheduler(const boost::shared_ptr<ITask> &ptr,boost::shared_ptr<Scheduler> scheduler_ptr,string scheduler_freq_event);
};

# endif

Taskmanager.cpp

# include "../include/TaskManager.h"
# include "../include/Scheduler.h"
# include <boost/asio.hpp>
# include "../include/zmqWrapper.h"
# include <zmq.hpp>
#include <boost/dll/import.hpp> // for import_alias
#include <boost/function.hpp>
# include <boost/bind.hpp>
# include <iostream>
# include <boost/function.hpp>
# include <boost/dll.hpp>
# include "../include/ConfigReader.h"
using namespace boost::dll;
# if __GNUC__
# include <unistd.h>

# endif
# include <boost/make_shared.hpp>
using namespace boost::asio;

int Task_Manager:: i = 0;
Task_Manager::Task_Manager()
{
    cout<<"in the task Manager"<<endl;
}

Task_Manager::~Task_Manager()
{

}

void Task_Manager::register_task(string sName,boost::shared_ptr<Scheduler> scheduler_ptr,string polling_frequency,int counter)
{

     boost::filesystem::path shared_library_path( ".");               

    typedef boost::shared_ptr<ITask> (pluginapi_create_t)();
    boost::function<pluginapi_create_t> creator;
    #if __GNUC__

        creator = boost::dll::import_alias<pluginapi_create_t>(             // type of imported symbol must be explicitly specified
        shared_library_path / "libmylib.so",                                            // path to library
        "create_plugin",                                                // symbol to import
        load_mode::append_decorations                              // do append extensions and prefixes
    );
    # endif


    # if __WIN32__

       creator = boost::dll::import_alias<pluginapi_create_t>(             // type of imported symbol must be explicitly specified
        shared_library_path / "libmylib.dll",                                            // path to library
        "create_plugin",                                                // symbol to import
        load_mode::append_decorations                              // do append extensions and prefixes
    );

    # endif


    boost::shared_ptr<ITask> plugin(creator());
    map<string,vector<string> > mp;

  this->ptr_list.push_back(make_pair(std::move(plugin),"snmp"));
    ConfigReader init(polling_frequency);

    mp = init.load_data_from_config(polling_frequency,counter);
    this->ptr_list[0].first->initialize(polling_frequency,mp);


    register_task_to_scheduler(boost::move(plugin),scheduler_ptr,polling_frequency);



}



void Task_Manager::register_task_to_scheduler(const boost::shared_ptr<ITask> &ptr,boost::shared_ptr<Scheduler> scheduler_ptr,string polling_frequency)
{

 scheduler_ptr->push_task_ptr_to_queue(std::move(ptr),polling_frequency);
}
void Task_Manager::get_data_from_remote_icmp()
{

  io_service service;
  ZMQ_WRAPPER wrapper(service);

  wrapper.connect_to_end_point("tcp://localhost:4040");

  wrapper.send_data_and_get_res_sync("10.10.0.105");

  service.run();

}

Whenever I am trying to execute the shared_ptr from the vector I am getting segmentation fault why is this behaving in this way even, when I am passing the shared_ptr to the scheduler for execution I am getting this error. But currently, I am pushing the shared_ptr to the vector of pair still the same error is there.


Solution

  • Task_Manager::register_task creates a reference to a local variable plugin and pushes that reference to a container that outlives the local variable.

    The other thing that is going on is move from plugin, twice. This makes little sense. Either your move is useless (the second one is, because there is no move construction or assignment involved), or your move destroys the variable (if there is move construction/assignment) and you cannot meaningfully refer to it afterwards.