fix(portal): recover AVDRMFrameDescriptor before encode_frame to prevent leak on error

This commit is contained in:
dailz
2026-05-22 13:20:04 +08:00
parent ffb36b7e0d
commit 2d448dcac5

View File

@@ -188,19 +188,22 @@ impl StatePortal {
(*hw_frame.as_mut_ptr()).pts = pts; (*hw_frame.as_mut_ptr()).pts = pts;
} }
// 8. Encode // 8. Recover the Boxed descriptor from raw_frame *before* encoding.
enc.encode_frame(&hw_frame)?; // av_hwframe_transfer_data has already imported the DMA-BUF into the
// VAAPI surface, so FFmpeg no longer references the descriptor struct.
// 9. Clean up: recover the Boxed descriptor from raw_frame to prevent leak. // Doing this before encode_frame ensures the descriptor is reclaimed
// Video::drop calls av_frame_free which does NOT free data[0]. // even if encode_frame returns early via `?`.
unsafe { unsafe {
let desc_ptr = (*raw_frame.as_ptr()).data[0] as *mut ffi::AVDRMFrameDescriptor; let desc_ptr = (*raw_frame.as_ptr()).data[0] as *mut ffi::AVDRMFrameDescriptor;
if !desc_ptr.is_null() { if !desc_ptr.is_null() {
let _ = Box::from_raw(desc_ptr); let _ = Box::from_raw(desc_ptr);
} }
} }
// raw_frame and hw_frame drop here via Video::drop → av_frame_free
// 9. Encode — safe to early-return via `?` now that descriptor is recovered.
enc.encode_frame(&hw_frame)?;
// raw_frame and hw_frame drop here via Video::drop → av_frame_free
Ok(()) Ok(())
} }