@@ -210,6 +210,7 @@ void hardsubx_process_frames_linear(struct lib_hardsubx_ctx *ctx, struct encoder
210210 if (dist < (0.2 * MIN (strlen (subtitle_text ), strlen (prev_subtitle_text ))))
211211 {
212212 dist = -1 ;
213+ free_rust_c_string (subtitle_text );
213214 subtitle_text = NULL ;
214215 prev_end_time = convert_pts_to_ms (ctx -> packet .pts , ctx -> format_ctx -> streams [ctx -> video_stream_id ]-> time_base );
215216 }
@@ -219,6 +220,7 @@ void hardsubx_process_frames_linear(struct lib_hardsubx_ctx *ctx, struct encoder
219220 add_cc_sub_text (ctx -> dec_sub , prev_subtitle_text , prev_begin_time , prev_end_time , "" , "BURN" , CCX_ENC_UTF_8 );
220221 encode_sub (enc_ctx , ctx -> dec_sub );
221222 prev_begin_time = prev_end_time + 1 ;
223+ free (prev_subtitle_text );
222224 prev_subtitle_text = NULL ;
223225 prev_sub_encoded = 1 ;
224226 prev_end_time = convert_pts_to_ms (ctx -> packet .pts , ctx -> format_ctx -> streams [ctx -> video_stream_id ]-> time_base );
@@ -252,6 +254,10 @@ void hardsubx_process_frames_linear(struct lib_hardsubx_ctx *ctx, struct encoder
252254 prev_sub_encoded = 0 ;
253255 }
254256 prev_packet_pts = ctx -> packet .pts ;
257+
258+ // Free subtitle_text from this iteration (was allocated by Rust in _process_frame_*_basic)
259+ free_rust_c_string (subtitle_text );
260+ subtitle_text = NULL ;
255261 }
256262 }
257263 av_packet_unref (& ctx -> packet );
@@ -263,6 +269,9 @@ void hardsubx_process_frames_linear(struct lib_hardsubx_ctx *ctx, struct encoder
263269 encode_sub (enc_ctx , ctx -> dec_sub );
264270 prev_sub_encoded = 1 ;
265271 }
272+
273+ // Cleanup
274+ free (prev_subtitle_text );
266275 activity_progress (100 , cur_sec / 60 , cur_sec % 60 );
267276}
268277
@@ -488,6 +497,7 @@ void process_hardsubx_linear_frames_and_normal_subs(struct lib_hardsubx_ctx *har
488497 if (dist < (0.2 * MIN (strlen (subtitle_text_hard ), strlen (prev_subtitle_text_hard ))))
489498 {
490499 dist = -1 ;
500+ free_rust_c_string (subtitle_text_hard );
491501 subtitle_text_hard = NULL ;
492502 prev_end_time_hard = convert_pts_to_ms (hard_ctx -> packet .pts , hard_ctx -> format_ctx -> streams [hard_ctx -> video_stream_id ]-> time_base );
493503 }
@@ -497,6 +507,7 @@ void process_hardsubx_linear_frames_and_normal_subs(struct lib_hardsubx_ctx *har
497507 add_cc_sub_text (hard_ctx -> dec_sub , prev_subtitle_text_hard , prev_begin_time_hard , prev_end_time_hard , "" , "BURN" , CCX_ENC_UTF_8 );
498508 encode_sub (enc_ctx , hard_ctx -> dec_sub );
499509 prev_begin_time_hard = prev_end_time_hard + 1 ;
510+ free (prev_subtitle_text_hard );
500511 prev_subtitle_text_hard = NULL ;
501512 prev_sub_encoded_hard = 1 ;
502513 prev_end_time_hard = convert_pts_to_ms (hard_ctx -> packet .pts , hard_ctx -> format_ctx -> streams [hard_ctx -> video_stream_id ]-> time_base );
@@ -517,6 +528,10 @@ void process_hardsubx_linear_frames_and_normal_subs(struct lib_hardsubx_ctx *har
517528 prev_sub_encoded_hard = 0 ;
518529 }
519530 prev_packet_pts_hard = hard_ctx -> packet .pts ;
531+
532+ // Free subtitle_text_hard from this iteration (allocated by Rust)
533+ free_rust_c_string (subtitle_text_hard );
534+ subtitle_text_hard = NULL ;
520535 }
521536 }
522537 }
@@ -529,6 +544,9 @@ void process_hardsubx_linear_frames_and_normal_subs(struct lib_hardsubx_ctx *har
529544 encode_sub (enc_ctx , hard_ctx -> dec_sub );
530545 prev_sub_encoded_hard = 1 ;
531546 }
547+
548+ // Cleanup
549+ free (prev_subtitle_text_hard );
532550 activity_progress (100 , cur_sec / 60 , cur_sec % 60 );
533551}
534552
0 commit comments