Skip to content

Commit

Permalink
[Peripherals] Fix segfault when calling CPeripheralBusAddon::Clear() …
Browse files Browse the repository at this point in the history
…twice
  • Loading branch information
garbear committed Mar 7, 2017
1 parent 1fe0a34 commit 948701d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
51 changes: 42 additions & 9 deletions xbmc/peripherals/bus/virtual/PeripheralBusAddon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,65 @@
using namespace PERIPHERALS;

CPeripheralBusAddon::CPeripheralBusAddon(CPeripherals *manager) :
CPeripheralBus("PeripBusAddon", manager, PERIPHERAL_BUS_ADDON)
CPeripheralBus("PeripBusAddon", manager, PERIPHERAL_BUS_ADDON),
m_bInitialized(false)
{
}

CPeripheralBusAddon::~CPeripheralBusAddon()
{
Clear();
}

void CPeripheralBusAddon::Initialise()
{
using namespace ADDON;

CAddonMgr::GetInstance().RegisterAddonMgrCallback(ADDON_PERIPHERALDLL, this);
CAddonMgr::GetInstance().Events().Subscribe(this, &CPeripheralBusAddon::OnEvent);
{
CSingleLock lock(m_initSection);
if (m_bInitialized)
return;

CPeripheralBus::Initialise();

CAddonMgr::GetInstance().RegisterAddonMgrCallback(ADDON_PERIPHERALDLL, this);
CAddonMgr::GetInstance().Events().Subscribe(this, &CPeripheralBusAddon::OnEvent);

m_bInitialized = true;
}

UpdateAddons();
}

CPeripheralBusAddon::~CPeripheralBusAddon()
void CPeripheralBusAddon::Clear()
{
using namespace ADDON;

CSingleLock lock(m_initSection);
if (!m_bInitialized)
return;

CAddonMgr::GetInstance().Events().Unsubscribe(this);
CAddonMgr::GetInstance().UnregisterAddonMgrCallback(ADDON_PERIPHERALDLL);

// stop everything before destroying any (loaded) addons
Clear();
PeripheralAddonVector addons;
{
CSingleLock lock(m_critSection);
addons = m_addons;
}

// destroy any (loaded) addons
for (const auto& addon : m_addons)
for (const auto& addon : addons)
addon->DestroyAddon();
addons.clear();

{
CSingleLock lock(m_critSection);
m_failedAddons.clear();
m_addons.clear();
}

m_failedAddons.clear();
m_addons.clear();
m_bInitialized = false;
}

bool CPeripheralBusAddon::GetAddon(const std::string &strId, ADDON::AddonPtr &addon) const
Expand Down
5 changes: 5 additions & 0 deletions xbmc/peripherals/bus/virtual/PeripheralBusAddon.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ namespace PERIPHERALS
virtual void Register(const PeripheralPtr& peripheral) override;
virtual void GetFeatures(std::vector<PeripheralFeature> &features) const override;
virtual bool HasFeature(const PeripheralFeature feature) const override;
virtual void Initialise() override;
virtual void Clear() override;
virtual PeripheralPtr GetPeripheral(const std::string &strLocation) const override;
virtual PeripheralPtr GetByPath(const std::string &strPath) const override;
virtual bool SupportsFeature(PeripheralFeature feature) const override;
Expand Down Expand Up @@ -102,6 +104,9 @@ namespace PERIPHERALS

bool PromptEnableAddons(const ADDON::VECADDONS& disabledAddons);

bool m_bInitialized;
CCriticalSection m_initSection;

PeripheralAddonVector m_addons;
PeripheralAddonVector m_failedAddons;
CCriticalSection m_critSection;
Expand Down

0 comments on commit 948701d

Please sign in to comment.