Commit 8983e903 authored by Brendan Long's avatar Brendan Long

Remove g_Instance in Bridge.cpp. This global was causing a stack overflow.

The loop was:

~DeviceSystemBridge()
Shutdown()
ShutdownInternal()
RegisterAdapterSignalHandlers()
~DeviceSystemBridge()

g_Instance seemed to be used to prevent the DeviceSystemBridge from going out of scope while the adapter listener was still alive. I handled this by notifying the listener in the bridge's destructor.
parent 1dd3b009
......@@ -8,8 +8,6 @@ namespace
{
DSB_DECLARE_LOGNAME(DeviceSystemBridge);
shared_ptr<Bridge::DeviceSystemBridge> g_Instance;
int32_t const kWaitTimeoutForAdapterOperation = 20000; //ms
std::string const kDeviceArrivalSignal = "Device_Arrival";
std::string const kDeviceArriaveDeviceHandle = "Device_Handle";
......@@ -25,11 +23,13 @@ namespace
Bridge::DeviceSystemBridge::DeviceSystemBridge(shared_ptr<IAdapter> const& adapter)
: m_alljoynInitialized(false)
, m_adapter(adapter)
, m_adapterSignalListener(new AdapterSignalListener(*this))
{
}
Bridge::DeviceSystemBridge::~DeviceSystemBridge()
{
m_adapterSignalListener->Shutdown();
Shutdown();
m_adapter.reset();
}
......@@ -58,11 +58,6 @@ Leave:
{
Shutdown();
}
else
{
g_Instance = shared_from_this();
m_adapterSignaListener = shared_ptr<AdapterSignalListener>(new AdapterSignalListener(shared_from_this()));
}
return st;
}
......@@ -117,8 +112,6 @@ QStatus Bridge::DeviceSystemBridge::Shutdown()
m_alljoynInitialized = false;
}
g_Instance.reset();
return st;
}
......@@ -201,15 +194,13 @@ QStatus Bridge::DeviceSystemBridge::RegisterAdapterSignalHandlers(bool isRegiste
{
QStatus st = ER_OK;
shared_ptr<IAdapterSignalListener> listener = dynamic_pointer_cast<IAdapterSignalListener>(g_Instance);
if (isRegister)
{
AdapterSignalVector signals = m_adapter->GetSignals();
for (AdapterSignalVector::const_iterator itr = signals.begin(); itr != signals.end(); ++itr)
{
Bridge::IAdapter::RegistrationHandle handle;
int ret = m_adapter->RegisterSignalListener((*itr)->GetName(), listener, NULL, handle);
int ret = m_adapter->RegisterSignalListener((*itr)->GetName(), m_adapterSignalListener, NULL, handle);
if (ret != 0)
{
DSBLOG_WARN("failed to register signal listener on adapter: 0x%x", st);
......
......@@ -54,25 +54,36 @@ namespace Bridge
class AdapterSignalListener : public IAdapterSignalListener
{
public:
AdapterSignalListener(shared_ptr<DeviceSystemBridge> const& parent)
: m_parent(parent) { }
AdapterSignalListener(DeviceSystemBridge& parent)
: m_parent(parent)
, m_shuttingDown(false)
{
}
virtual void AdapterSignalHandler(IAdapterSignal const& signal, void* argp)
{
shared_ptr<DeviceSystemBridge> dsb = m_parent.lock();
if (dsb)
dsb->OnAdapterSignal(signal, argp);
if (m_shuttingDown)
{
return;
}
m_parent.OnAdapterSignal(signal, argp);
}
void Shutdown()
{
m_shuttingDown = true;
}
private:
weak_ptr<DeviceSystemBridge> m_parent;
DeviceSystemBridge& m_parent;
bool m_shuttingDown;
};
private:
bool m_alljoynInitialized;
shared_ptr<IAdapter> m_adapter;
BridgeDeviceList m_deviceList;
shared_ptr<AdapterSignalListener> m_adapterSignaListener;
shared_ptr<AdapterSignalListener> m_adapterSignalListener;
std::vector<IAdapter::RegistrationHandle> m_registeredSignalListeners;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment