-
Notifications
You must be signed in to change notification settings - Fork 1k
fix: window memory leak - memory increase while recording #1381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: window memory leak - memory increase while recording #1381
Conversation
WalkthroughMemory management optimization in H.264 video encoding MediaFoundation binding. The code now directly consumes samples from output buffers instead of cloning, reducing unnecessary allocations while maintaining identical control flow and error handling semantics. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
crates/enc-mediafoundation/src/video/h264.rs(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.rs
📄 CodeRabbit inference engine (AGENTS.md)
**/*.rs: Format Rust code usingrustfmtand ensure all Rust code passes workspace-level clippy lints.
Rust modules should be named with snake_case, and crate directories should be in kebab-case.
Files:
crates/enc-mediafoundation/src/video/h264.rs
crates/*/src/**/*
📄 CodeRabbit inference engine (AGENTS.md)
Rust crates should place tests within the
src/and/or a siblingtests/directory for each crate insidecrates/*.
Files:
crates/enc-mediafoundation/src/video/h264.rs
🧠 Learnings (1)
📚 Learning: 2025-10-28T08:39:42.230Z
Learnt from: Brendonovich
Repo: CapSoftware/Cap PR: 1305
File: crates/recording/src/output_pipeline/macos.rs:80-90
Timestamp: 2025-10-28T08:39:42.230Z
Learning: In `crates/recording/src/output_pipeline/macos.rs`, the `AVFoundationMp4Muxer` intentionally holds the `Mutex<MP4Encoder>` lock during retry attempts in `send_video_frame()` and `send_audio_frame()`. This blocking behavior is correct because frame processing must happen sequentially to prevent audio and video frames from being interleaved incorrectly in the encoder.
Applied to files:
crates/enc-mediafoundation/src/video/h264.rs
🔇 Additional comments (1)
crates/enc-mediafoundation/src/video/h264.rs (1)
441-444: Excellent memory management fix!Using
take()instead of cloning the sample is the correct approach to prevent memory leaks. This transfers ownership from the output buffer and avoids incrementing COM reference counts unnecessarily, which directly addresses the streaming memory leak described in the PR.
| return Err( | ||
| windows::core::Error::new( | ||
| windows::core::HRESULT(0), | ||
| "Too many consecutive empty samples" | ||
| ) | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a proper error HRESULT instead of 0.
HRESULT(0) represents S_OK (success), which is semantically incorrect for an error condition. Consider using a standard error code like E_FAIL or E_UNEXPECTED to properly indicate failure.
Apply this diff:
- return Err(
- windows::core::Error::new(
- windows::core::HRESULT(0),
- "Too many consecutive empty samples"
- )
- );
+ return Err(windows::core::Error::new(
+ windows::Win32::Foundation::E_FAIL,
+ "Too many consecutive empty samples",
+ ));🤖 Prompt for AI Agents
In crates/enc-mediafoundation/src/video/h264.rs around lines 448 to 453, the
code constructs an error using windows::core::HRESULT(0) (which is S_OK).
Replace the zero HRESULT with a proper failure code (for example
windows::Win32::Foundation::E_FAIL or E_UNEXPECTED) when creating the
windows::core::Error; update imports if necessary so the chosen HRESULT constant
is available and use that constant when constructing the Error to correctly
indicate failure.
Summary by CodeRabbit