C++使用共享_ptr,this和多态性读取Multimap读取的sigsegv

发布于 2025-01-29 18:32:17 字数 1641 浏览 4 评论 0原文

因此,我正在尝试为任务编码调度程序。

我的调度程序正在使用如下:

typedef std::shared_ptr<Task> TaskPtr;
typedef std::pair <long, TaskPtr> Task_Pair;
[...]
std::multimap<long, TaskPtr> tasks;

我这样做时,我这样做是为每个任务使用特定类,全部从任务继承。

当我第一次插入它们时(如下):

this->insertTask(new WifiParseTask());

使用以下方法:

void Scheduler::insertTask(Task* task) {
    TaskPtr ptr(task);
    this->tasks.insert(Task_Pair(task->getNextStartingMoment(), ptr));
}

它的工作正常,WifiparStask执行良好。

问题是,我希望一项任务在停止时将自己重新放在任务中。我使用的是:
- 在调度程序中启动一个任务:

void Scheduler::dispatch(std::multimap<long, std::shared_ptr<Task>>::iterator taskPair) {
    long startTime = std::floor(taskPair->first/10);
    if(startTime <= App::getCurrentTime()) {
        TaskPtr task = taskPair->second;
        this->tasks.erase(taskPair);
        task->run();
        App::getInstance()->getScheduler()->setStatus(Status::ON);
    }
}

- 任务完成时:

void WifiParseTask::stop() {
    super::stop();
    std::cout << "Stopped task Wifi Parsing" << "\n";
    App::getInstance()->getScheduler()->insert(this); 
}

每当我的调度程序试图再次执行任务时,这会给我一个Sigsev(因此,当它运行时,使用“ this”插入了“ dispatch”时)。我猜这是插入中的“此”,以某种方式做错了什么。我找到了一种解决方法:

App::getInstance()->getScheduler()->insert(new WifiParseTask(this->startingMoment));

这是某种程度上的事情,除了它不使用相同的任务,而是创建了它的“副本”,并且可以正常工作。问题是,我看不出当我称之“此”时它不起作用,因为“此”应该是WifiparSetask的一个实例,就像“新的WifiparSetask”一样,那么为什么“这个”不起作用?我想念什么吗?

谢谢大家。

So, I'm trying to code a scheduler for tasks.

My scheduler is using a multimap like follows:

typedef std::shared_ptr<Task> TaskPtr;
typedef std::pair <long, TaskPtr> Task_Pair;
[...]
std::multimap<long, TaskPtr> tasks;

I did this as I'm using specific classes for each Tasks, all inheriting from Task.

When I first insert them (as follow):

this->insertTask(new WifiParseTask());

Using this method:

void Scheduler::insertTask(Task* task) {
    TaskPtr ptr(task);
    this->tasks.insert(Task_Pair(task->getNextStartingMoment(), ptr));
}

It works just fine and the WifiParseTask executes well.

The problem is, I want a task to put itself back in the tasks multimap when it stops. What I use is:
-To start a task in the scheduler:

void Scheduler::dispatch(std::multimap<long, std::shared_ptr<Task>>::iterator taskPair) {
    long startTime = std::floor(taskPair->first/10);
    if(startTime <= App::getCurrentTime()) {
        TaskPtr task = taskPair->second;
        this->tasks.erase(taskPair);
        task->run();
        App::getInstance()->getScheduler()->setStatus(Status::ON);
    }
}

-When the task finishes:

void WifiParseTask::stop() {
    super::stop();
    std::cout << "Stopped task Wifi Parsing" << "\n";
    App::getInstance()->getScheduler()->insert(this); 
}

And this gives me a sigsev whenever my scheduler tries to execute the Task again (so when it runs "dispatch" on the pair inserted using "this"). I'm guessing it comes from "this" in the insert, that somehow does something wrong. I found a way around:

App::getInstance()->getScheduler()->insert(new WifiParseTask(this->startingMoment));

This somehow does a similar thing, except that instead of using still the same task, it creates a "copy" of it, and it works. The thing is, I can't see why it's not working when I call "this", as "this" should be an instance of WifiParseTask, exactly like a "new WifiParseTask" is, so why is "this" not working ? Am I missing something ?

Thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文