[Download] fix crash while getting map element 44/165744/2
authorLukasz Bardeli <l.bardeli@samsung.com>
Wed, 3 Jan 2018 12:09:24 +0000 (13:09 +0100)
committerLukasz Bardeli <l.bardeli@samsung.com>
Wed, 3 Jan 2018 13:33:27 +0000 (13:33 +0000)
There may be situation when while destroying DownloadInfoPtr in DownloadInstance
destructor on second thread static method is called and check if instance is exist
and later try to read info from DownloadInfoPtr. To avoid this situation lock_guard
is moved just before part of code responsible for destroying DownloadInfoPtr

[Verification] Code compiles without error. 100% passrate

Change-Id: I20b0b0fc1b021d91d2f7daaf36500e4c2b83766c

src/download/download_instance.cc

index e34d1aa5926018e2695fbcf9ea3f7bbee72ffa2c..52923da869ee9be93313e8d9feaf02bd535f7cf7 100644 (file)
@@ -61,6 +61,9 @@ DownloadInstance::DownloadInstance() {
 DownloadInstance::~DownloadInstance() {
   ScopeLogger();
   int ret;
+
+  std::lock_guard<std::mutex> lock(instances_mutex_);
+
   for (DownloadCallbackMap::iterator it = download_callbacks.begin();
        it != download_callbacks.end(); ++it) {
     DownloadInfoPtr diPtr = it->second->instance->diMap[it->second->callbackId];
@@ -89,7 +92,6 @@ DownloadInstance::~DownloadInstance() {
     delete (it->second);
   }
 
-  std::lock_guard<std::mutex> lock(instances_mutex_);
   for (auto it = instances_.begin(); it != instances_.end(); it++) {
     if (*it == this) {
       instances_.erase(it);