Skip to content

Fix null pointer reference on MacOS #174

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

keathmilligan
Copy link

@keathmilligan keathmilligan commented Aug 25, 2025

Fixes #173 null pointer reference when capturing a single window on MacOS.

Use ScreenCapturekit directly to get window dimensions.

Summary by CodeRabbit

  • New Features
    • None.
  • Bug Fixes
    • Improved accuracy of window scale and dimensions on macOS, reducing blurry or incorrectly sized captures.
    • Better handling across multiple displays and high‑DPI setups.
    • Added safe fallbacks to prevent failures when a window or display can’t be resolved.
  • Refactor
    • Switched macOS window detection and sizing to a more robust system for greater stability and compatibility.
  • Chores
    • No user-facing API changes.

Use ScreenCapturekit directly to get window dimensions.
Copy link

coderabbitai bot commented Aug 25, 2025

Walkthrough

Replaced Cocoa-based window scale and dimension retrieval with ScreenCaptureKit queries in the mac target. Window branches now find the window by id from SC content, compute scale from the containing display's mode, and compute dimensions from the SC frame with fallbacks. Display branches unchanged.

Changes

Cohort / File(s) Summary of Changes
macOS target: SC-based window handling
src/targets/mac/mod.rs
Replaced unsafe NSApp/NSWindow logic with ScreenCaptureKit lookups for Target::Window in get_scale_factor and get_target_dimensions; compute scale from display_mode.pixel_width / width; compute dims from SC frame * scale; added fallbacks (scale = 1.0, dims = 800×600); Target::Display paths unchanged.

Sequence Diagram(s)

sequenceDiagram
  actor Caller
  participant MacTarget as mac::get_scale_factor
  participant SC as ScreenCaptureKit

  Caller->>MacTarget: get_scale_factor(Target::Window)
  MacTarget->>SC: query current content (windows, displays)
  SC-->>MacTarget: windows list, displays list
  alt window found and display resolved
    MacTarget->>MacTarget: compute scale = pixel_width / width
    MacTarget-->>Caller: scale
  else fallback
    MacTarget-->>Caller: 1.0
  end
Loading
sequenceDiagram
  actor Caller
  participant MacTarget as mac::get_target_dimensions
  participant SC as ScreenCaptureKit

  Caller->>MacTarget: get_target_dimensions(Target::Window)
  MacTarget->>SC: query current content (windows)
  SC-->>MacTarget: windows list
  alt window found
    MacTarget->>MacTarget: frame = window.frame * scale
    MacTarget-->>Caller: (width, height)
  else fallback
    MacTarget-->>Caller: (800, 600)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
Fix null pointer in Mac single-window capture in get_target_dimensions (#[173])

Poem

I hop through panes where pixels play,
Swapped Cocoa sips for SC’s ray.
No nulls to nibble, bounds are clear,
Frames and scales now appear sincere.
A rabbit cheers — capture’s here! 🐇✨

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between a23a356 and 1a8e669.

📒 Files selected for processing (1)
  • src/targets/mac/mod.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/targets/mac/mod.rs
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/targets/mac/mod.rs (2)

134-134: Naming nit: cg_win_id is misleading (this is an SC window id).

Rename to avoid confusion with CoreGraphics ids.

Example:

-            let cg_win_id = window.raw_handle;
+            let sc_win_id = window.raw_handle;

…and update references accordingly within this block.


89-123: Optional: reduce duplicate SC enumeration and improve locality.

Both get_scale_factor and get_target_dimensions call ShareableContent::current() and iterate windows/displays. Consider a small helper to resolve “window -> containing display mode” once, returning (frame_in_points, mode) so both scale and pixel size derive from the same snapshot. This also avoids TOCTOU between two independent enumerations.

If you want, I can draft a minimal helper like:

  • fn resolve_window_and_display(window_id) -> Option<(CGRect, cg::DirectDisplayId, cg::DisplayMode)>
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c03f15a and a23a356.

📒 Files selected for processing (1)
  • src/targets/mac/mod.rs (2 hunks)
🔇 Additional comments (1)
src/targets/mac/mod.rs (1)

89-123: Confirm coordinate space consistency between SCWindow.frame and CGDisplay bounds.

You’re comparing sc_window.frame() against CGDirectDisplay bounds to choose the containing display. On macOS this should be in global display coordinates (points), but issues can arise with rotated displays, mirroring, or unusual arrangements. Please validate on multi-display + rotation setups.

I can provide a small diagnostic that logs frame and the matched display bounds at runtime to confirm assumptions if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Null pointer reference on MacOS capturing single window
1 participant