处理 shared_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();

}

每当我尝试从向量中执行 shared_ptr 时,我都会收到分段错误,为什么即使在我将 shared_ptr 传递给调度程序执行时也会出现这种情况我我收到这个错误。但是目前,我将 shared_ptr 推到 pair 的向量仍然存在相同的错误。

Task_Manager::register_task 创建对局部变量的引用 plugin 并将该引用推送到比局部变量寿命更长的容器。

发生的另一件事是 move 来自 plugin 两次 。这没有什么意义。要么你的 move 没用(第二个是,因为没有涉及移动构造或赋值),要么你的 move destroys 变量(如果有移动 construction/assignment) 之后你就不能有意义地引用它了。