Use SafeStringStream instead of std::stringstream to try to fix random crashes on...
[dcpomatic.git] / src / lib / update.cc
index 6e5f92edc9eee06d9ea86a56502312bcfd1e6d08..7bec061e9292e15d044c360e8c13d60dc6a0faab 100644 (file)
 */
 
 #include <string>
-#include <sstream>
 #include <boost/algorithm/string.hpp>
 #include <curl/curl.h>
 #include <libcxml/cxml.h>
+#include <libdcp/raw_convert.h>
 #include "update.h"
 #include "version.h"
 #include "ui_signaller.h"
+#include "safe_stringstream.h"
 
 #define BUFFER_SIZE 1024
 
 using std::cout;
 using std::min;
 using std::string;
-using std::stringstream;
-using boost::lexical_cast;
+using libdcp::raw_convert;
 
 UpdateChecker* UpdateChecker::_instance = 0;
 
@@ -48,6 +48,7 @@ UpdateChecker::UpdateChecker ()
        , _curl (0)
        , _state (NOT_RUN)
        , _emits (0)
+       , _to_do (0)
 {
        curl_global_init (CURL_GLOBAL_ALL);
        _curl = curl_easy_init ();
@@ -76,15 +77,19 @@ void
 UpdateChecker::run ()
 {
        boost::mutex::scoped_lock lm (_process_mutex);
+       _to_do++;
        _condition.notify_one ();
 }
 
 void
 UpdateChecker::thread ()
 {
-       while (1) {
+       while (true) {
                boost::mutex::scoped_lock lock (_process_mutex);
-               _condition.wait (lock);
+               while (_to_do == 0) {
+                       _condition.wait (lock);
+               }
+               --_to_do;
                lock.unlock ();
                
                try {
@@ -97,10 +102,9 @@ UpdateChecker::thread ()
                        }
                        
                        _buffer[_offset] = '\0';
-                       stringstream s;
-                       s << _buffer;
+                       string s (_buffer);
                        cxml::Document doc ("Update");
-                       doc.read_stream (s);
+                       doc.read_string (s);
                        
                        {
                                boost::mutex::scoped_lock lm (_data_mutex);
@@ -115,12 +119,12 @@ UpdateChecker::thread ()
                                current_pre = true;
                        }
                        
-                       float current_float = lexical_cast<float> (current);
+                       float current_float = raw_convert<float> (current);
                        if (current_pre) {
                                current_float -= 0.005;
                        }
                        
-                       if (current_float < lexical_cast<float> (_stable)) {
+                       if (current_float < raw_convert<float> (_stable)) {
                                set_state (YES);
                        } else {
                                set_state (NO);