我正在尝试编写类似类的升压信号(作为学习 C++ 的练习)。我发现当我使用时enable_shared_from_this
,我总是在析构函数中崩溃。这是代码(我使用的是 vs2012 更新 2):
对此有何评论?应该引起这个问题shared_from_this()
,因为当我取消注释中的 ( auto this_ = shared_from_this();
)行时signal_connection::disconnect
,一切正常。
我知道在调用之前它必须至少有一个有效的 shared_ptr shared_from_this()
。我的代码应该满足这一点。
代码:
#include <memory>
#include <map>
class slot_manager;
class signal_connection: public std::enable_shared_from_this<signal_connection>
{
public:
signal_connection(slot_manager* manager)
:manager_(manager)
{}
public:
void disconnect() ;
private:
slot_manager* manager_;
};
class slot_manager
{
public:
typedef std::shared_ptr<signal_connection> connection_type;
typedef std::map<connection_type, int> map_type;
typedef map_type::value_type map_value_type;
public:
void connect(int slot)
{
std::shared_ptr<signal_connection> c(new signal_connection(this));
slots_.insert(map_value_type(c, slot));
}
~slot_manager()
{
auto iter = slots_.begin();
map_type::iterator iter2 = slots_.end();
while (iter != slots_.end())
{
iter2 = iter++;
iter2->first->disconnect();
}
}
void disconnect(std::shared_ptr<signal_connection> connection)
{
auto c = slots_.find(connection);
if (c != slots_.end())
{
slots_.erase(c);
}
}
protected:
map_type slots_;
};
void signal_connection::disconnect()
{
if (manager_ != nullptr)
{
//auto this_ = shared_from_this();
manager_->disconnect(shared_from_this());
manager_ = nullptr;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
slot_manager x;
x.connect(1);
return 0;
}