From 412eda6beeb1d03f6c136a8505c522c8df87a6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mal=C3=BD?= <madcatxster@devoid-pointer.net> Date: Sat, 7 Jan 2023 15:55:45 +0100 Subject: [PATCH] Unlock mutex when SpaceGroup cache object construction fails --- clipper/core/clipper_instance.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/clipper/core/clipper_instance.cpp b/clipper/core/clipper_instance.cpp index 86c26e3..039472f 100644 --- a/clipper/core/clipper_instance.cpp +++ b/clipper/core/clipper_instance.cpp @@ -42,6 +42,18 @@ #include "clipper_instance.h" +template<class Lockable> +class _MutexLocker { +public: + explicit _MutexLocker() + { + Lockable::mutex.lock(); + } + ~_MutexLocker() + { + Lockable::mutex.unlock(); + } +}; namespace clipper { @@ -90,7 +102,8 @@ namespace clipper { template<class T> typename ObjectCache<T>::Reference ObjectCache<T>::cache( const typename T::Key& key ) { - T::mutex.lock(); + _MutexLocker<T> lk; + std::pair<int,T>* ptr = NULL; // find existing data for ( int i = 0; i < cache_.size(); i++ ) @@ -104,9 +117,9 @@ namespace clipper { if ( mode_ == NORMAL ) // NORMAL: replace unreferenced for ( int i = 0; i < cache_.size(); i++ ) if ( cache_[i]->first == 0 ) { - ptr = cache_[i]; - ptr->second = T(key); - break; + ptr = cache_[i]; + ptr->second = T(key); + break; } // otherwise add new if ( ptr == NULL ) { @@ -115,7 +128,6 @@ namespace clipper { } } Reference result( ptr ); - T::mutex.unlock(); return result; // we have a ref to the new obj, so it is thread safe } -- GitLab