From 2e8c74e8aa3b7ed16a2c65cac2c54f4bf01cf1c1 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Thu, 7 Oct 2021 13:34:00 -0500 Subject: [PATCH] Prevent crash when smallest frame detected in memory object is NULL --- src/CacheDisk.cpp | 11 ++++++----- src/CacheMemory.cpp | 11 ++++++----- src/Qt/VideoCacheThread.cpp | 13 ++++++------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/CacheDisk.cpp b/src/CacheDisk.cpp index 6d1241ed..43882c82 100644 --- a/src/CacheDisk.cpp +++ b/src/CacheDisk.cpp @@ -294,7 +294,6 @@ std::shared_ptr CacheDisk::GetSmallestFrame() { // Create a scoped lock, to protect the cache from multiple threads const GenericScopedLock lock(*cacheCriticalSection); - std::shared_ptr f; // Loop through frame numbers std::deque::iterator itr; @@ -305,10 +304,12 @@ std::shared_ptr CacheDisk::GetSmallestFrame() smallest_frame = *itr; } - // Return frame - f = GetFrame(smallest_frame); - - return f; + // Return frame (if any) + if (smallest_frame != -1) { + return GetFrame(smallest_frame); + } else { + return NULL; + } } // Gets the maximum bytes value diff --git a/src/CacheMemory.cpp b/src/CacheMemory.cpp index 62dc9957..6d51e533 100644 --- a/src/CacheMemory.cpp +++ b/src/CacheMemory.cpp @@ -168,7 +168,6 @@ std::shared_ptr CacheMemory::GetSmallestFrame() { // Create a scoped lock, to protect the cache from multiple threads const GenericScopedLock lock(*cacheCriticalSection); - std::shared_ptr f; // Loop through frame numbers std::deque::iterator itr; @@ -179,10 +178,12 @@ std::shared_ptr CacheMemory::GetSmallestFrame() smallest_frame = *itr; } - // Return frame - f = GetFrame(smallest_frame); - - return f; + // Return frame (if any) + if (smallest_frame != -1) { + return GetFrame(smallest_frame); + } else { + return NULL; + } } // Gets the maximum bytes value diff --git a/src/Qt/VideoCacheThread.cpp b/src/Qt/VideoCacheThread.cpp index 371d3ac0..6c4f1ac0 100644 --- a/src/Qt/VideoCacheThread.cpp +++ b/src/Qt/VideoCacheThread.cpp @@ -86,6 +86,7 @@ namespace openshot void VideoCacheThread::run() { // Types for storing time durations in whole and fractional milliseconds + std::shared_ptr smallest_frame = NULL; using ms = std::chrono::milliseconds; using double_ms = std::chrono::duration; @@ -107,13 +108,11 @@ namespace openshot ZmqLogger::Instance()->AppendDebugMethod("VideoCacheThread::run (cache frame)", "position", position, "current_display_frame", current_display_frame, "max_concurrent_frames", max_concurrent_frames, "needed_frames", (position - current_display_frame)); // Force the frame to be generated - if (reader->GetCache()->GetSmallestFrame()) { - int64_t smallest_cached_frame = reader->GetCache()->GetSmallestFrame()->number; - if (smallest_cached_frame > current_display_frame) { - // Cache position has gotten too far away from current display frame. - // Reset the position to the current display frame. - position = current_display_frame; - } + smallest_frame = reader->GetCache()->GetSmallestFrame(); + if (smallest_frame && smallest_frame->number > current_display_frame) { + // Cache position has gotten too far away from current display frame. + // Reset the position to the current display frame. + position = current_display_frame; } reader->GetFrame(position); }