diff --git a/src/lib_ccx/ccx_decoders_common.c b/src/lib_ccx/ccx_decoders_common.c index 01c30f7e..ca323c37 100644 --- a/src/lib_ccx/ccx_decoders_common.c +++ b/src/lib_ccx/ccx_decoders_common.c @@ -241,7 +241,21 @@ void dinit_cc_decode(struct lib_cc_decode **ctx) freep(&bitmap->data1); } } + /* Free any leftover XDS strings that weren't processed by the encoder */ + if (lctx->dec_sub.type == CC_608 && lctx->dec_sub.data) + { + struct eia608_screen *data = (struct eia608_screen *)lctx->dec_sub.data; + for (int i = 0; i < lctx->dec_sub.nb_data; i++, data++) + { + if (data->format == SFORMAT_XDS && data->xds_str) + { + freep(&data->xds_str); + } + } + } freep(&lctx->dec_sub.data); + /* Note: xds_ctx is freed in general_loop.c, mp4.c etc. during normal processing. + Don't free it here as it may cause double-free if already freed elsewhere. */ freep(ctx); } diff --git a/src/lib_ccx/ccx_decoders_xds.c b/src/lib_ccx/ccx_decoders_xds.c index dfd94e21..159700cb 100644 --- a/src/lib_ccx/ccx_decoders_xds.c +++ b/src/lib_ccx/ccx_decoders_xds.c @@ -175,6 +175,8 @@ void xdsprint(struct cc_subtitle *sub, struct ccx_decoders_xds_context *ctx, con if (n > -1 && n < size) { write_xds_string(sub, ctx, p, n); + /* Note: Don't free(p) here - the pointer is stored in data->xds_str + and will be freed by the encoder or decoder cleanup code */ return; } /* Else try again with more space. */ diff --git a/src/lib_ccx/ccx_encoders_common.c b/src/lib_ccx/ccx_encoders_common.c index ac2af699..13547753 100644 --- a/src/lib_ccx/ccx_encoders_common.c +++ b/src/lib_ccx/ccx_encoders_common.c @@ -924,6 +924,11 @@ int encode_sub(struct encoder_ctx *context, struct cc_subtitle *sub) // After adding delay, if start/end time is lower than 0, then continue with the next subtitle if (data->start_time < 0 || data->end_time <= 0) { + // Free XDS string if skipping to avoid memory leak + if (data->format == SFORMAT_XDS && data->xds_str) + { + freep(&data->xds_str); + } continue; } diff --git a/src/lib_ccx/ccx_encoders_spupng.c b/src/lib_ccx/ccx_encoders_spupng.c index 1bfe3f24..09fca63f 100644 --- a/src/lib_ccx/ccx_encoders_spupng.c +++ b/src/lib_ccx/ccx_encoders_spupng.c @@ -570,10 +570,8 @@ int write_image(struct pixel_t *buffer, FILE *fp, int width, int height) png_write_end(png_ptr, NULL); finalise: - if (info_ptr != NULL) - png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); if (png_ptr != NULL) - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + png_destroy_write_struct(&png_ptr, &info_ptr); if (row != NULL) free(row);