← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 3016: Prevent a crash when releasing a hook as it is being called

 

------------------------------------------------------------
revno: 3016
author: Crise
committer: iceman50 <bdcdevel@xxxxxxxxx>
branch nick: dcplusplus
timestamp: Sun 2012-07-29 17:46:08 -0500
message:
  Prevent a crash when releasing a hook as it is being called
modified:
  dcpp/PluginManager.cpp


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/PluginManager.cpp'
--- dcpp/PluginManager.cpp	2012-07-08 03:48:50 +0000
+++ dcpp/PluginManager.cpp	2012-07-29 22:46:08 +0000
@@ -328,15 +328,17 @@
 		return NULL;
 	}
 	auto& hook = i->second;
-
-	auto subscription = make_unique<HookSubscriber>();
-	auto pSub = subscription.get();
-	subscription->hookProc = hookProc;
-	subscription->common = pCommon;
-	subscription->owner = hook->guid;
-	hook->subscribers.push_back(move(subscription));
-
-	return pSub;
+	{
+		Lock l(hook->cs);	// not strictly needed, for additions, due to characteristic of std::vector<..>
+		auto subscription = make_unique<HookSubscriber>();
+		auto pSub = subscription.get();
+		subscription->hookProc = hookProc;
+		subscription->common = pCommon;
+		subscription->owner = hook->guid;
+		hook->subscribers.push_back(move(subscription));
+
+		return pSub;
+	}
 }
 
 bool PluginManager::runHook(PluginHook* hook, dcptr_t pObject, dcptr_t pData) {
@@ -373,11 +375,13 @@
 		return 0;
 	}
 	auto& hook = i->second;
-
-	hook->subscribers.erase(std::remove_if(hook->subscribers.begin(), hook->subscribers.end(),
-		[subscription](const unique_ptr<HookSubscriber>& sub) { return sub.get() == subscription; }), hook->subscribers.end());
-
-	return hook->subscribers.size();
+	{
+		Lock l(hook->cs);
+		hook->subscribers.erase(std::remove_if(hook->subscribers.begin(), hook->subscribers.end(),
+			[subscription](const unique_ptr<HookSubscriber>& sub) { return sub.get() == subscription; }), hook->subscribers.end());
+
+		return hook->subscribers.size();
+	}
 }
 
 // Plugin configuration