Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segfault caused by destruction order of Event and Connection #234

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions events/include/ignition/common/Event.hh
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ namespace ignition
private: class EventConnection
{
/// \brief Constructor
public: EventConnection(const bool _on, const std::function<T> &_cb)
: callback(_cb)
public: EventConnection(const bool _on, const std::function<T> &_cb,
const ConnectionPtr &_publicConn)
: callback(_cb), publicConnection(_publicConn)
{
// Windows Visual Studio 2012 does not have atomic_bool constructor,
// so we have to set "on" using operator=
Expand All @@ -169,6 +170,11 @@ namespace ignition

/// \brief Callback function
public: std::function<T> callback;

/// \brief A weak pointer to the Connection pointer returned by Connect.
/// This is used to clear the Connection's Event pointer during
/// destruction of an Event.
public: std::weak_ptr<Connection> publicConnection;
};

/// \def EvtConnectionMap
Expand Down Expand Up @@ -197,6 +203,16 @@ namespace ignition
template<typename T, typename N>
EventT<T, N>::~EventT()
{
// Clear the Event pointer on all connections so that they are not
// accessed after this Event is destructed.
for (auto &conn : this->connections)
{
auto publicCon = conn.second->publicConnection.lock();
if (publicCon)
{
publicCon->event = nullptr;
}
}
this->connections.clear();
}

Expand All @@ -211,8 +227,10 @@ namespace ignition
auto const &iter = this->connections.rbegin();
index = iter->first + 1;
}
this->connections[index].reset(new EventConnection(true, _subscriber));
return ConnectionPtr(new Connection(this, index));
auto connection = ConnectionPtr(new Connection(this, index));
this->connections[index].reset(
new EventConnection(true, _subscriber, connection));
return connection;
}

/// \brief Get the number of connections.
Expand All @@ -234,6 +252,14 @@ namespace ignition
if (it != this->connections.end())
{
it->second->on = false;
// The destructor of std::function seems to crashes if the function it
// points to is in a shared library and has been unloaded by the time
// the destructor is invoked. It's not clear whether this is a bug in
// the implementation of std::function or not. To avoid the crash,
// we call the destructor here by setting `callback = nullptr` because
// it is likely that EventT::Disconnect is called before the shared
// library is unloaded via Connection::~Connection.
it->second->callback = nullptr;
this->connectionsToRemove.push_back(it);
}
}
Expand Down
15 changes: 15 additions & 0 deletions events/src/Event_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ TEST_F(EventTest, typeid_test)
EXPECT_NE(typeid(Event3).name(), typeid(Event2).name());
}

/////////////////////////////////////////////////
TEST_F(EventTest, DestructionOrder)
{
auto evt = std::make_unique<common::EventT<void()>>();
common::ConnectionPtr conn = evt->Connect(callback);
evt->Signal();
evt.reset();
// Sleep to avoid warning about deleting a connection right after creation.
IGN_SLEEP_MS(1);

// Check that this doesn't segfault.
conn.reset();
SUCCEED();
}

/////////////////////////////////////////////////
int main(int argc, char **argv)
{
Expand Down