Fix crashes on x-thread signal emission.
[dcpomatic.git] / src / lib / writer.cc
index d2116362edd157359acf079b5fe17f595028ef62..1a11a482bf0b0155d948848474e5cc18333b0296 100644 (file)
@@ -366,21 +366,24 @@ try
                        }
 
                        DCPOMATIC_ASSERT (i != _queue.rend());
-                       QueueItem qi = *i;
-
                        ++_pushed_to_disk;
-                       
                        lock.unlock ();
 
+                       /* i is valid here, even though we don't hold a lock on the mutex,
+                          since list iterators are unaffected by insertion and only this
+                          thread could erase the last item in the list.
+                       */
+
                        LOG_GENERAL (
                                "Writer full (awaiting %1 [last eye was %2]); pushes %3 to disk",
                                _last_written_frame + 1,
-                               _last_written_eyes, qi.frame
+                               _last_written_eyes, i->frame
                                );
                        
-                       qi.encoded->write (_film, qi.frame, qi.eyes);
+                       i->encoded->write (_film, i->frame, i->eyes);
+                       
                        lock.lock ();
-                       qi.encoded.reset ();
+                       i->encoded.reset ();
                        --_queued_full_in_memory;
                }
 
@@ -512,18 +515,24 @@ Writer::finish ()
                        boost::filesystem::path const from = i->file.get_value_or (liberation);
                        _subtitle_content->add_font (i->id, from.leaf().string ());
 
-                       boost::filesystem::path to = _film->dir (_film->dcp_name ()) / from.leaf();
+                       boost::filesystem::path to = _film->dir (_film->dcp_name ()) / _subtitle_content->id ();
+                       boost::filesystem::create_directories (to, ec);
+                       if (ec) {
+                               throw FileError (_("Could not create directory"), to);
+                       }
+
+                       to /= from.leaf();
 
                        boost::system::error_code ec;
                        boost::filesystem::copy_file (from, to, ec);
-                       if (!ec) {
-                               dcp.add (shared_ptr<dcp::Font> (new dcp::Font (to)));
-                       } else {
-                               LOG_WARNING_NC (String::compose ("Could not copy font %1 to DCP", from.string ()));
+                       if (ec) {
+                               throw FileError ("Could not copy font to DCP", from);
                        }
+
+                       dcp.add (shared_ptr<dcp::Font> (new dcp::Font (to)));
                }
 
-               _subtitle_content->write_xml (_film->dir (_film->dcp_name ()) / _film->subtitle_xml_filename ());
+               _subtitle_content->write_xml (_film->dir (_film->dcp_name ()) / _subtitle_content->id () / _film->subtitle_xml_filename ());
                reel->add (shared_ptr<dcp::ReelSubtitleAsset> (
                                   new dcp::ReelSubtitleAsset (
                                           _subtitle_content,