Skip to content
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

fix(macos): add reference count to layer to prevent use after free #304

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/platform/macos/system/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ impl Device {
transaction::set_disable_actions(true);

let superlayer = CALayer::new();
let _: () = msg_send![superlayer.id(), retain];
Copy link
Member

Choose a reason for hiding this comment

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

Does CALayer::new return a CALayer with a reference? If so, won't this leak the CALayer? If that's the case, maybe the layer reference count increase should happen in the interface between surfman and winit.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that could happen if the native widget is from other sources and they don't clean up their window entirely like winit.
How about a platform-specific method like retain_layer? In this case, I can call it for winit usage.

Copy link
Member

Choose a reason for hiding this comment

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

I think this might be fixed by servo/core-foundation-rs#546.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah thanks! I'll track the coacoa release and close this one.

native_widget.view.0.setLayer(superlayer.id());
native_widget.view.0.setWantsLayer(YES);

Expand All @@ -209,6 +210,7 @@ impl Device {

let opaque = native_widget.opaque;
let layer = CALayer::new();
let _: () = msg_send![layer.id(), retain];
let layer_size = CGSize::new(logical_size.width as f64, logical_size.height as f64);
layer.set_frame(&CGRect::new(&CG_ZERO_POINT, &layer_size));
layer.set_contents(front_surface.obj as id);
Expand Down