Skip to content

Commit 937089a

Browse files
authored
Merge pull request #236 from OpenShot/memory-fixes
Fixing Memory Leaks (lots of them)
2 parents 17a2258 + 4a3985e commit 937089a

24 files changed

+120
-48
lines changed

include/CacheBase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ namespace openshot {
6060
/// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
6161
CacheBase(int64_t max_bytes);
6262

63+
virtual ~CacheBase();
64+
6365
/// @brief Add a Frame to the cache
6466
/// @param frame The openshot::Frame object needing to be cached.
6567
virtual void Add(std::shared_ptr<Frame> frame) = 0;

include/CacheMemory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace openshot {
7171
CacheMemory(int64_t max_bytes);
7272

7373
// Default destructor
74-
~CacheMemory();
74+
virtual ~CacheMemory();
7575

7676
/// @brief Add a Frame to the cache
7777
/// @param frame The openshot::Frame object needing to be cached.

include/Clip.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ namespace openshot {
112112

113113
// File Reader object
114114
ReaderBase* reader;
115-
bool manage_reader;
115+
116+
/// If we allocated a reader, we store it here to free it later
117+
/// (reader member variable itself may have been replaced)
118+
ReaderBase* allocated_reader;
116119

117120
/// Adjust frame number minimum value
118121
int64_t adjust_frame_number_minimum(int64_t frame_number);
@@ -160,7 +163,7 @@ namespace openshot {
160163
Clip(ReaderBase* new_reader);
161164

162165
/// Destructor
163-
~Clip();
166+
virtual ~Clip();
164167

165168
/// @brief Add an effect to the clip
166169
/// @param effect Add an effect to the clip. An effect can modify the audio or video of an openshot::Frame.

include/ClipBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ namespace openshot {
6969

7070
/// Constructor for the base clip
7171
ClipBase() { };
72+
virtual ~ClipBase();
7273

7374
// Compare a clip using the Position() property
7475
bool operator< ( ClipBase& a) { return (Position() < a.Position()); }

include/DummyReader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ namespace openshot
6464
/// Constructor for DummyReader.
6565
DummyReader(Fraction fps, int width, int height, int sample_rate, int channels, float duration);
6666

67+
virtual ~DummyReader();
68+
6769
/// Close File
6870
void Close();
6971

include/FFmpegReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ namespace openshot {
243243
FFmpegReader(string path, bool inspect_reader);
244244

245245
/// Destructor
246-
~FFmpegReader();
246+
virtual ~FFmpegReader();
247247

248248
/// Close File
249249
void Close();

include/FFmpegUtilities.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@
209209
#define AV_FORMAT_NEW_STREAM(oc, st_codec, av_codec, av_st) av_st = avformat_new_stream(oc, NULL);\
210210
if (!av_st) \
211211
throw OutOfMemory("Could not allocate memory for the video stream.", path); \
212-
c = avcodec_alloc_context3(av_codec); \
213-
st_codec = c; \
214-
av_st->codecpar->codec_id = av_codec->id;
212+
avcodec_get_context_defaults3(av_st->codec, av_codec); \
213+
c = av_st->codec; \
214+
st_codec = c;
215215
#define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) avcodec_parameters_from_context(av_stream->codecpar, av_codec);
216216
#elif LIBAVFORMAT_VERSION_MAJOR >= 55
217217
#define AV_REGISTER_ALL av_register_all();

include/Frame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ namespace openshot
159159
Frame& operator= (const Frame& other);
160160

161161
/// Destructor
162-
~Frame();
162+
virtual ~Frame();
163163

164164
/// Add (or replace) pixel data to the frame (based on a solid color)
165165
void AddColor(int new_width, int new_height, string new_color);

include/FrameMapper.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ namespace openshot
170170
FrameMapper(ReaderBase *reader, Fraction target_fps, PulldownType target_pulldown, int target_sample_rate, int target_channels, ChannelLayout target_channel_layout);
171171

172172
/// Destructor
173-
~FrameMapper();
173+
virtual ~FrameMapper();
174174

175175
/// Change frame rate or audio mapping details
176176
void ChangeMapping(Fraction target_fps, PulldownType pulldown, int target_sample_rate, int target_channels, ChannelLayout target_channel_layout);
@@ -213,6 +213,9 @@ namespace openshot
213213
/// Get the current reader
214214
ReaderBase* Reader();
215215

216+
/// Set the current reader
217+
void Reader(ReaderBase *new_reader) { reader = new_reader; }
218+
216219
/// Resample audio and map channels (if needed)
217220
void ResampleMappedAudio(std::shared_ptr<Frame> frame, int64_t original_frame_number);
218221

include/QtImageReader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ namespace openshot
8181
/// when you are inflating the object using JSON after instantiating it.
8282
QtImageReader(string path, bool inspect_reader);
8383

84+
virtual ~QtImageReader();
85+
8486
/// Close File
8587
void Close();
8688

0 commit comments

Comments
 (0)