diff Plugin/Cache/CacheScheduler.cpp @ 147:70d1fe6d6309

Avoid hard crash if not enough memory
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Nov 2016 12:43:58 +0100
parents 3809121c3290
children 5dc54316d68b
line wrap: on
line diff
--- a/Plugin/Cache/CacheScheduler.cpp	Wed Nov 09 12:20:01 2016 +0100
+++ b/Plugin/Cache/CacheScheduler.cpp	Wed Nov 09 12:43:58 2016 +0100
@@ -112,52 +112,65 @@
       {
         std::auto_ptr<DynamicString> prefetch(that->queue_.Dequeue(500));
 
-        if (prefetch.get() != NULL)
+        try
         {
+          if (prefetch.get() != NULL)
           {
-            boost::mutex::scoped_lock lock(that->invalidatedMutex_);
-            that->invalidated_ = false;
-            that->prefetching_ = prefetch->GetValue();
-          }
+            {
+              boost::mutex::scoped_lock lock(that->invalidatedMutex_);
+              that->invalidated_ = false;
+              that->prefetching_ = prefetch->GetValue();
+            }
+
+            {
+              boost::mutex::scoped_lock lock(that->cacheMutex_);
+              if (that->cache_.IsCached(that->bundleIndex_, prefetch->GetValue()))
+              {
+                // This item is already cached
+                continue;
+              }
+            }
+
+            std::string content;
 
-          {
-            boost::mutex::scoped_lock lock(that->cacheMutex_);
-            if (that->cache_.IsCached(that->bundleIndex_, prefetch->GetValue()))
+            try
+            {
+              if (!that->factory_.Create(content, prefetch->GetValue()))
+              {
+                // The factory cannot generate this item
+                continue;
+              }
+            }
+            catch (...)
             {
-              // This item is already cached
+              // Exception
               continue;
             }
+
+            {
+              boost::mutex::scoped_lock lock(that->invalidatedMutex_);
+              if (that->invalidated_)
+              {
+                // This item has been invalidated
+                continue;
+              }
+              
+              {
+                boost::mutex::scoped_lock lock2(that->cacheMutex_);
+                that->cache_.Store(that->bundleIndex_, prefetch->GetValue(), content);
+              }
+            }
           }
-
-          std::string content;
-
-          try
-          {
-            if (!that->factory_.Create(content, prefetch->GetValue()))
-            {
-              // The factory cannot generate this item
-              continue;
-            }
-          }
-          catch (...)
-          {
-            // Exception
-            continue;
-          }
-
-          {
-            boost::mutex::scoped_lock lock(that->invalidatedMutex_);
-            if (that->invalidated_)
-            {
-              // This item has been invalidated
-              continue;
-            }
-              
-            {
-              boost::mutex::scoped_lock lock2(that->cacheMutex_);
-              that->cache_.Store(that->bundleIndex_, prefetch->GetValue(), content);
-            }
-          }
+        }
+        catch (std::bad_alloc&)
+        {
+          OrthancPluginLogError(that->cache_.GetPluginContext(), 
+                                "Not enough memory for the prefetcher of the Web viewer to work");
+        }
+        catch (...)
+        {
+          OrthancPluginLogError(that->cache_.GetPluginContext(), 
+                                "Unhandled native exception inside the prefetcher of the Web viewer");
         }
       }
     }