【发布时间】:2019-11-04 00:42:44
【问题描述】:
编译这段代码后 我收到指示存在的错误消息 发生双重删除;我的问题是到底是什么 这段代码错了吗?问题出在哪里?
#include <iostream>
#include <memory>
class Manager;
class Interface {
protected:
friend class InputListener;
bool flag_;
};
class InputListener {
public:
InputListener(std::shared_ptr<Interface> manager_ptr) {
manager_ptr_ = std::move(manager_ptr);
std::async(std::launch::async, &InputListener::Run, this);
}
void Run() {
char c;
std::cin >> c;
manager_ptr_->flag_ = true;
}
private:
std::shared_ptr<Interface> manager_ptr_;
};
class Manager : public Interface {
public:
Manager()
: listener_ptr_{
std::make_unique<InputListener>(std::shared_ptr<Manager>(this))} {}
void Run() {
while (true) {
if (Interface::flag_)
break;
}
}
private:
std::unique_ptr<InputListener> listener_ptr_;
};
int main() {
Manager m;
m.Run();
return 0;
}
为什么这段代码会产生这个错误? munmap_chunk(): 无效指针 中止(核心转储)
【问题讨论】:
标签: c++ unique-ptr