From d8a2e55855b50eda28ec7d394449274f5e085bd6 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Wed, 5 Feb 2014 14:52:23 +0000 Subject: Fix some coverity-reported stuff. --- src/lib/ffmpeg.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/lib/ffmpeg.cc') diff --git a/src/lib/ffmpeg.cc b/src/lib/ffmpeg.cc index d3653e311..b7ae04b06 100644 --- a/src/lib/ffmpeg.cc +++ b/src/lib/ffmpeg.cc @@ -146,7 +146,8 @@ void FFmpeg::setup_video () { boost::mutex::scoped_lock lm (_mutex); - + + assert (_video_stream >= 0); AVCodecContext* context = _format_context->streams[_video_stream]->codec; AVCodec* codec = avcodec_find_decoder (context->codec_id); -- cgit v1.2.3 From e4b65ba64c319281d6bff15e950ebce697c9621a Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sun, 9 Feb 2014 22:21:31 +0000 Subject: Fix 1 crash on low memory. --- ChangeLog | 2 ++ src/lib/ffmpeg.cc | 3 ++- src/lib/image.cc | 8 ++++---- src/lib/util.cc | 13 +++++++++++++ src/lib/util.h | 1 + src/wx/film_viewer.cc | 10 +++++++++- 6 files changed, 31 insertions(+), 6 deletions(-) (limited to 'src/lib/ffmpeg.cc') diff --git a/ChangeLog b/ChangeLog index 1febaac51..4f0bce552 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2014-02-09 Carl Hetherington + * Some small bits of increased low-memory stability. + * Version 1.64.7 released. 2014-02-08 Carl Hetherington diff --git a/src/lib/ffmpeg.cc b/src/lib/ffmpeg.cc index b7ae04b06..fae9baa2b 100644 --- a/src/lib/ffmpeg.cc +++ b/src/lib/ffmpeg.cc @@ -26,6 +26,7 @@ extern "C" { #include "ffmpeg.h" #include "ffmpeg_content.h" #include "exceptions.h" +#include "util.h" #include "i18n.h" @@ -85,7 +86,7 @@ FFmpeg::setup_general () av_register_all (); _file_group.set_paths (_ffmpeg_content->paths ()); - _avio_buffer = static_cast (av_malloc (_avio_buffer_size)); + _avio_buffer = static_cast (wrapped_av_malloc (_avio_buffer_size)); _avio_context = avio_alloc_context (_avio_buffer, _avio_buffer_size, 0, this, avio_read_wrapper, 0, avio_seek_wrapper); _format_context = avformat_alloc_context (); _format_context->pb = _avio_context; diff --git a/src/lib/image.cc b/src/lib/image.cc index 95bf2b04d..4722563c4 100644 --- a/src/lib/image.cc +++ b/src/lib/image.cc @@ -498,13 +498,13 @@ Image::Image (AVPixelFormat p, libdcp::Size s, bool aligned) void Image::allocate () { - _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *)); + _data = (uint8_t **) wrapped_av_malloc (4 * sizeof (uint8_t *)); _data[0] = _data[1] = _data[2] = _data[3] = 0; - _line_size = (int *) av_malloc (4 * sizeof (int)); + _line_size = (int *) wrapped_av_malloc (4 * sizeof (int)); _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0; - _stride = (int *) av_malloc (4 * sizeof (int)); + _stride = (int *) wrapped_av_malloc (4 * sizeof (int)); _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0; for (int i = 0; i < components(); ++i) { @@ -520,7 +520,7 @@ Image::allocate () seem to mind. The nasty + 1 in this malloc makes sure there is always a byte for that instruction to read safely. */ - _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i) + 1); + _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * lines (i) + 1); } } diff --git a/src/lib/util.cc b/src/lib/util.cc index edb202df2..190bb42b1 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef DCPOMATIC_POSIX #include #include @@ -92,6 +93,7 @@ using std::istream; using std::numeric_limits; using std::pair; using std::cout; +using std::bad_alloc; using std::streampos; using boost::shared_ptr; using boost::thread; @@ -916,3 +918,14 @@ fit_ratio_within (float ratio, libdcp::Size full_frame) return libdcp::Size (full_frame.width, rint (full_frame.width / ratio)); } + +void * +wrapped_av_malloc (size_t s) +{ + void* p = av_malloc (s); + if (!p) { + throw bad_alloc (); + } + return p; +} + diff --git a/src/lib/util.h b/src/lib/util.h index 7dcd920b7..a229bbfc9 100644 --- a/src/lib/util.h +++ b/src/lib/util.h @@ -120,6 +120,7 @@ extern float get_required_float (std::multimap const & extern std::string get_required_string (std::multimap const & kv, std::string k); extern int get_optional_int (std::multimap const & kv, std::string k); extern std::string get_optional_string (std::multimap const & kv, std::string k); +extern void* wrapped_av_malloc (size_t); /** @class Socket * @brief A class to wrap a boost::asio::ip::tcp::socket with some things diff --git a/src/wx/film_viewer.cc b/src/wx/film_viewer.cc index fbca835c2..e24583d6c 100644 --- a/src/wx/film_viewer.cc +++ b/src/wx/film_viewer.cc @@ -45,6 +45,7 @@ using std::min; using std::max; using std::cout; using std::list; +using std::bad_alloc; using std::make_pair; using boost::shared_ptr; using boost::dynamic_pointer_cast; @@ -126,7 +127,14 @@ FilmViewer::set_film (shared_ptr f) return; } - _player = f->make_player (); + try { + _player = f->make_player (); + } catch (bad_alloc) { + error_dialog (this, _("There is not enough free memory to do that.")); + _film.reset (); + return; + } + _player->disable_audio (); _player->Video.connect (boost::bind (&FilmViewer::process_video, this, _1, _2, _5)); _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1)); -- cgit v1.2.3