-
Notifications
You must be signed in to change notification settings - Fork 56
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
Crash when callback is disconnected in a dll #78
Comments
I noticed you're not disconnecting the slot properly. There is a
disconnect method on the connection object.
…On Fri, Sep 6, 2024, 7:03 PM Yassine Sen ***@***.***> wrote:
When using signals2 across multiple DDLs, everything works fine until the
dlls gets unloaded, which results in crash the next time we dispatch the
signal.
I found this stackoverflow post
<https://stackoverflow.com/questions/28703432/access-violation-when-using-boostsignals2-and-unloading-dlls>
which describes the same issue i'm having.
The test sample:
Shared.cpp:
#include <boost/dll.hpp>
#include <boost/signals2.hpp>
#include <iostream>
struct SomeInterface
{
boost::signals2::signal<void()> signal;
};
boost::signals2::connection con;
void init(SomeInterface* iface)
{
con = iface->signal.connect([] { std::cout << "Hello, Shared!" << std::endl; });
}
void shutdown(SomeInterface* iface)
{
iface->signal.disconnect(con);
}
BOOST_DLL_AUTO_ALIAS(init);BOOST_DLL_AUTO_ALIAS(shutdown);
Host.cpp:
#include <boost/dll.hpp>
#include <boost/signals2.hpp>
#include <iostream>
struct SomeInterface
{
boost::signals2::signal<void()> signal;
};
int main()
{
SomeInterface* iface = new SomeInterface;
iface->signal.connect([] { std::cout << "Hello, Host!" << std::endl; });
{
boost::dll::shared_library dll("Shared", boost::dll::load_mode::append_decorations);
auto init = dll.get<void (*)(SomeInterface*)>("init");
auto shutdown = dll.get<void (*)(SomeInterface*)>("shutdown");
init(iface);
iface->signal();
shutdown(iface);
}
iface->signal();
return 0;
}
—
Reply to this email directly, view it on GitHub
<#78>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AARKTX5FGNHJXW5HSF7FQGDZVIYFHAVCNFSM6AAAAABNZNN6Q2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGUYTCMRZHA2TSNQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Updated the sample, thanks for pointing it out, however that was not the issue
|
As stated on the stackoverflow post, one could wrap the signal around an interface to delegate the connect() method to the owner of the signal, however a fix to this issue would be nicer than the extra indirection for a workaround. using Connection = boost::signals2::connection;
using ScopedConnection = boost::signals2::scoped_connection;
template<typename... SigTy> class Signal
{
public:
using base_type = boost::signals2::signal<SigTy...>;
using slot_type = typename base_type::slot_type;
using slot_ex = typename base_type::extended_slot_type;
private:
struct ISignal
{
virtual ~ISignal() = default;
virtual Connection Connect(const slot_type& slot) = 0;
virtual Connection ConnectEx(const slot_ex& slot) = 0;
};
struct SignalImpl : ISignal
{
[[nodiscard]] Connection Connect(const slot_type& slot) override
{
return m_Signal.connect(slot);
}
[[nodiscard]] Connection ConnectEx(const slot_ex& slot) override
{
return m_Signal.connect_extended(slot);
}
base_type m_Signal;
};
public:
Connection Connect(const slot_type& slot)
{
return m_Signal->Connect(slot);
}
Connection ConnectEx(const slot_ex& slot)
{
return m_Signal->ConnectEx(slot);
}
template<typename... Args> auto Invoke(Args&&... args)
{
auto impl = static_cast<SignalImpl*>(m_Signal.get());
return impl->m_Signal(std::forward<Args>(args)...);
}
private:
std::unique_ptr<ISignal> m_Signal = std::make_unique<SignalImpl>();
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When using signals2 across multiple DDLs, everything works fine until the dlls gets unloaded, which results in crash the next time we dispatch the signal.
I found this stackoverflow post which describes the same issue i'm having.
The test sample:
Shared.cpp:
Host.cpp:
The text was updated successfully, but these errors were encountered: