Skip to content
Snippets Groups Projects
Verified Commit 7ba85493 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Resolve some memory leaks

parent 3ce91376
Branches
No related tags found
No related merge requests found
...@@ -3,8 +3,8 @@ use std::path::{Path, PathBuf}; ...@@ -3,8 +3,8 @@ use std::path::{Path, PathBuf};
use ffmpeg_dev::sys as ffi; use ffmpeg_dev::sys as ffi;
use fraction::Fraction; use fraction::Fraction;
use num_traits::FromPrimitive;
use media_time::MediaTimeError; use media_time::MediaTimeError;
use num_traits::FromPrimitive;
use thiserror::Error; use thiserror::Error;
use crate::enums::*; use crate::enums::*;
...@@ -21,7 +21,7 @@ pub enum StringError { ...@@ -21,7 +21,7 @@ pub enum StringError {
#[error("String is unexpectedly null")] #[error("String is unexpectedly null")]
NullError, NullError,
#[error(transparent)] #[error(transparent)]
Utf8Error(#[from] std::str::Utf8Error) Utf8Error(#[from] std::str::Utf8Error),
} }
fn native_string(ptr: *const std::os::raw::c_char) -> Result<String, StringError> { fn native_string(ptr: *const std::os::raw::c_char) -> Result<String, StringError> {
...@@ -48,7 +48,7 @@ pub enum AVFormatContextError { ...@@ -48,7 +48,7 @@ pub enum AVFormatContextError {
#[error("Path {0} contains null byte")] #[error("Path {0} contains null byte")]
PathContainsNull(PathBuf, #[source] std::ffi::NulError), PathContainsNull(PathBuf, #[source] std::ffi::NulError),
#[error("Opening media file {0} failed")] #[error("Opening media file {0} failed")]
OpenInputFailed(PathBuf, #[source] AVError) OpenInputFailed(PathBuf, #[source] AVError),
} }
impl AVFormatContext { impl AVFormatContext {
...@@ -58,7 +58,7 @@ impl AVFormatContext { ...@@ -58,7 +58,7 @@ impl AVFormatContext {
Err(AVAllocError::AllocFailed("AVFormatContext".to_string())) Err(AVAllocError::AllocFailed("AVFormatContext".to_string()))
} else { } else {
Ok(AVFormatContext { base }) Ok(AVFormatContext { base })
} };
} }
pub fn open_input(&mut self, path: &Path) -> Result<(), AVFormatContextError> { pub fn open_input(&mut self, path: &Path) -> Result<(), AVFormatContextError> {
...@@ -95,6 +95,7 @@ impl AVFormatContext { ...@@ -95,6 +95,7 @@ impl AVFormatContext {
} }
pub fn read_frame(&mut self, packet: &mut AVPacket) -> Result<(), AVFrameError> { pub fn read_frame(&mut self, packet: &mut AVPacket) -> Result<(), AVFrameError> {
unsafe { ffi::av_packet_unref(packet.base) }
AVError::from_errno(unsafe { ffi::av_read_frame(self.base, packet.base) }) AVError::from_errno(unsafe { ffi::av_read_frame(self.base, packet.base) })
.map_err(|err| AVFrameError::DecodingFailed(packet.stream_index(), packet.pts(), err)) .map_err(|err| AVFrameError::DecodingFailed(packet.stream_index(), packet.pts(), err))
} }
...@@ -171,7 +172,7 @@ impl AVBuffer { ...@@ -171,7 +172,7 @@ impl AVBuffer {
Err(AVAllocError::AllocFailed("AVBufferContext".to_string())) Err(AVAllocError::AllocFailed("AVBufferContext".to_string()))
} else { } else {
Ok(AVBuffer { base, size }) Ok(AVBuffer { base, size })
} };
} }
pub fn empty() -> Self { pub fn empty() -> Self {
...@@ -190,6 +191,14 @@ impl AVBuffer { ...@@ -190,6 +191,14 @@ impl AVBuffer {
} }
} }
impl Drop for AVBuffer {
fn drop(&mut self) {
unsafe {
ffi::av_free(self.base as *mut std::ffi::c_void)
}
}
}
pub struct AVPacket { pub struct AVPacket {
base: *mut ffi::AVPacket, base: *mut ffi::AVPacket,
} }
...@@ -207,7 +216,7 @@ impl AVPacket { ...@@ -207,7 +216,7 @@ impl AVPacket {
Err(AVAllocError::AllocFailed("AVPacket".to_string())) Err(AVAllocError::AllocFailed("AVPacket".to_string()))
} else { } else {
Ok(AVPacket { base }) Ok(AVPacket { base })
} };
} }
fn as_ref(&self) -> &ffi::AVPacket { fn as_ref(&self) -> &ffi::AVPacket {
...@@ -229,7 +238,10 @@ impl AVPacket { ...@@ -229,7 +238,10 @@ impl AVPacket {
impl Drop for AVPacket { impl Drop for AVPacket {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { ffi::av_packet_free(&mut self.base) } unsafe {
ffi::av_packet_unref(self.base);
ffi::av_packet_free(&mut self.base);
}
} }
} }
...@@ -243,7 +255,7 @@ pub enum AVFrameError { ...@@ -243,7 +255,7 @@ pub enum AVFrameError {
#[error(transparent)] #[error(transparent)]
AllocFailed(#[from] AVAllocError), AllocFailed(#[from] AVAllocError),
#[error("Decoding a frame from packet of stream {0} at timestamp {1} failed")] #[error("Decoding a frame from packet of stream {0} at timestamp {1} failed")]
DecodingFailed(i32, i64, #[source] AVError) DecodingFailed(i32, i64, #[source] AVError),
} }
impl AVFrame { impl AVFrame {
...@@ -252,8 +264,8 @@ impl AVFrame { ...@@ -252,8 +264,8 @@ impl AVFrame {
return if base.is_null() { return if base.is_null() {
Err(AVAllocError::AllocFailed("AVFrame".to_string())) Err(AVAllocError::AllocFailed("AVFrame".to_string()))
} else { } else {
Ok(AVFrame { base, buffer: AVBuffer::empty(), }) Ok(AVFrame { base, buffer: AVBuffer::empty() })
} };
} }
pub fn init(&mut self, width: i32, height: i32, format: AVPixelFormat) -> Result<(), AVFrameError> { pub fn init(&mut self, width: i32, height: i32, format: AVPixelFormat) -> Result<(), AVFrameError> {
...@@ -345,7 +357,10 @@ impl AVFrame { ...@@ -345,7 +357,10 @@ impl AVFrame {
impl Drop for AVFrame { impl Drop for AVFrame {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { ffi::av_frame_free(&mut self.base) } unsafe {
ffi::av_frame_unref(self.base);
ffi::av_frame_free(&mut self.base);
}
} }
} }
...@@ -511,6 +526,7 @@ impl AVCodecContext { ...@@ -511,6 +526,7 @@ impl AVCodecContext {
} }
pub fn out_frame(&mut self, frame: &mut AVFrame) -> Result<(), AVCodecContextError> { pub fn out_frame(&mut self, frame: &mut AVFrame) -> Result<(), AVCodecContextError> {
unsafe { ffi::av_frame_unref(frame.base) }
AVError::from_errno(unsafe { ffi::avcodec_receive_frame(self.base, frame.base) }) AVError::from_errno(unsafe { ffi::avcodec_receive_frame(self.base, frame.base) })
.map_err(|err| AVCodecContextError::FrameError(err)) .map_err(|err| AVCodecContextError::FrameError(err))
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment