diff --git a/c_src/xav/decoder.c b/c_src/xav/decoder.c index f372356..ff50813 100644 --- a/c_src/xav/decoder.c +++ b/c_src/xav/decoder.c @@ -79,4 +79,14 @@ int decoder_decode(struct Decoder *decoder, AVPacket *pkt, AVFrame *frame) { } return 0; +} + +void decoder_free(struct Decoder *decoder) { + if (decoder->swr_ctx != NULL) { + swr_free(&decoder->swr_ctx); + } + + if (decoder->c != NULL) { + avcodec_free_context(&decoder->c); + } } \ No newline at end of file diff --git a/c_src/xav/xav_nif.c b/c_src/xav/xav_nif.c index 565861a..dd53440 100644 --- a/c_src/xav/xav_nif.c +++ b/c_src/xav/xav_nif.c @@ -186,10 +186,12 @@ ERL_NIF_TERM decode(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { } av_frame_free(&frame); + av_packet_free(&pkt); return xav_nif_ok(env, frame_term); } void free_reader(ErlNifEnv *env, void *obj) { + XAV_LOG_DEBUG("Freeing Reader object"); struct Reader *reader = (struct Reader *)obj; reader_free(reader); } @@ -197,9 +199,7 @@ void free_reader(ErlNifEnv *env, void *obj) { void free_decoder(ErlNifEnv *env, void *obj) { XAV_LOG_DEBUG("Freeing Decoder object"); struct Decoder *decoder = (struct Decoder *)obj; - if (decoder->c) { - avcodec_free_context(&decoder->c); - } + decoder_free(decoder); } static ErlNifFunc xav_funcs[] = {{"new_reader", 3, new_reader},