Skip to content

Commit

Permalink
Fix a few remaining double-frees
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 5, 2023
1 parent ba252ff commit 039d599
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 45 deletions.
4 changes: 1 addition & 3 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ impl View {
center_x: LayoutAnchorX::center(view),
center_y: LayoutAnchorY::center(view),

layer: Layer::wrap(unsafe {
msg_send![view, layer]
}),
layer: Layer::from_id(unsafe { msg_send_id![view, layer] }),

objc: ObjcProperty::retain(view),
}
Expand Down
11 changes: 0 additions & 11 deletions src/appkit/toolbar/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ impl ToolbarItem {
}
}

pub(crate) fn wrap(item: id) -> Self {
ToolbarItem {
identifier: "".to_string(),
objc: unsafe { Id::new(item).unwrap() },
button: None,
segmented_control: None,
image: None,
handler: None
}
}

/// Sets the title for this item.
pub fn set_title(&mut self, title: &str) {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion src/defaults/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl UserDefaults {
//
// For context: https://nshipster.com/type-encodings/
if NSNumber::is(result) {
let number = NSNumber::wrap(result);
let number = NSNumber::retain(result);

return match number.objc_type() {
"c" => Some(Value::Bool(number.as_bool())),
Expand Down
6 changes: 0 additions & 6 deletions src/foundation/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ impl NSNumber {
NSNumber(unsafe { Id::retain(data).unwrap() })
}

/// If we're vended an NSNumber from a method (e.g, `NSUserDefaults` querying) we might want to
/// wrap it while we figure out what to do with it. This does that.
pub fn wrap(data: id) -> Self {
NSNumber(unsafe { Id::new(data).unwrap() })
}

/// Constructs a `numberWithBool` instance of `NSNumber` and retains it.
pub fn bool(value: bool) -> Self {
NSNumber(unsafe {
Expand Down
4 changes: 2 additions & 2 deletions src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core_foundation::base::TCFType;

use objc::rc::{Id, Shared};
use objc::runtime::{Class, Object};
use objc::{msg_send, sel};
use objc::{msg_send, msg_send_id, sel};

use crate::color::Color;
use crate::foundation::{id, nil, NSArray, NSString, NO, YES};
Expand Down Expand Up @@ -142,7 +142,7 @@ impl ImageView {
#[cfg(feature = "autolayout")]
center_y: LayoutAnchorY::center(view),

layer: Layer::wrap(unsafe { msg_send![view, layer] }),
layer: Layer::from_id(unsafe { msg_send_id![view, layer] }),

objc: ObjcProperty::retain(view)
}
Expand Down
20 changes: 9 additions & 11 deletions src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use core_graphics::base::CGFloat;

use objc::{class, msg_send, sel};
use objc::rc::{Id, Shared};
use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};

use crate::foundation::id;
use crate::utils::properties::ObjcProperty;
Expand All @@ -35,30 +37,26 @@ use crate::utils::properties::ObjcProperty;
#[derive(Clone, Debug)]
pub struct Layer {
/// The underlying layer pointer.
pub objc: ObjcProperty
pub objc: Id<Object, Shared>
}

impl Layer {
/// Creates a new `CALayer` and retains it.
pub fn new() -> Self {
Layer {
objc: ObjcProperty::retain(unsafe { msg_send![class!(CALayer), new] })
objc: unsafe { msg_send_id![class!(CALayer), new] }
}
}

/// Wraps an existing (already retained) `CALayer`.
pub fn wrap(layer: id) -> Self {
Layer {
objc: ObjcProperty::from_retained(layer)
}
/// Wraps an existing `CALayer`.
pub fn from_id(objc: Id<Object, Shared>) -> Self {
Layer { objc }
}

/// Sets the corner radius (for all four corners).
///
/// Note that for performance sensitive contexts, you might want to apply a mask instead.
pub fn set_corner_radius(&self, radius: f64) {
self.objc.with_mut(|obj| unsafe {
let _: () = msg_send![obj, setCornerRadius: radius as CGFloat];
});
let _: () = unsafe { msg_send![&self.objc, setCornerRadius: radius as CGFloat] };
}
}
4 changes: 2 additions & 2 deletions src/text/label/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use core_foundation::base::TCFType;

use objc::rc::{Id, Shared};
use objc::runtime::{Class, Object};
use objc::{msg_send, sel};
use objc::{msg_send, msg_send_id, sel};

use crate::color::Color;
use crate::foundation::{id, nil, NSArray, NSInteger, NSString, NSUInteger, NO, YES};
Expand Down Expand Up @@ -251,7 +251,7 @@ impl Label {
#[cfg(feature = "autolayout")]
center_y: LayoutAnchorY::center(view),

layer: Layer::wrap(unsafe { msg_send![view, layer] }),
layer: Layer::from_id(unsafe { msg_send_id![view, layer] }),

objc: ObjcProperty::retain(view)
}
Expand Down
5 changes: 0 additions & 5 deletions src/utils/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ impl ObjcProperty {
ObjcProperty(Rc::new(RefCell::new(unsafe { Id::retain(obj).unwrap() })))
}

/// Given an Objective-C object, retains it and wraps it as a `Property`.
pub fn from_retained(obj: id) -> Self {
ObjcProperty(Rc::new(RefCell::new(unsafe { Id::new(obj).unwrap() })))
}

/// Runs a handler with mutable access for the underlying Objective-C object.
///
/// Note that this is mutable access from the Rust side; we make every effort to ensure things are valid
Expand Down
4 changes: 2 additions & 2 deletions src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
//! For more information on Autolayout, view the module or check out the examples folder.
use objc::runtime::{Class, Object};
use objc::{msg_send, sel};
use objc::{msg_send, msg_send_id, sel};

use crate::color::Color;
use crate::foundation::{id, nil, NSArray, NSInteger, NSString, NO, YES};
Expand Down Expand Up @@ -217,7 +217,7 @@ impl View {
#[cfg(feature = "autolayout")]
center_y: LayoutAnchorY::center(view),

layer: Layer::wrap(unsafe { msg_send![view, layer] }),
layer: Layer::from_id(unsafe { msg_send_id![view, layer] }),

#[cfg(all(feature = "appkit", target_os = "macos"))]
animator: ViewAnimatorProxy::new(view),
Expand Down
2 changes: 1 addition & 1 deletion src/webview/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::utils::load;
/// Called when an `alert()` from the underlying `WKWebView` is fired. Will call over to your
/// `WebViewController`, where you should handle the event.
extern "C" fn alert<T: WebViewDelegate>(_: &Object, _: Sel, _: id, _: id, _: id, complete: id) {
//let alert = NSString::wrap(s).to_str();
//let alert = NSString::retain(s).to_str();

// @TODO: This is technically (I think?) a private method, and there's some other dance that
// needs to be done here involving taking the pointer/invoke/casting... but this is fine for
Expand Down
2 changes: 1 addition & 1 deletion src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl WebView {
#[cfg(feature = "autolayout")]
center_y: LayoutAnchorY::center(view),

layer: Layer::wrap(unsafe { msg_send![view, layer] }),
layer: Layer::from_id(unsafe { msg_send_id![view, layer] }),

objc: ObjcProperty::retain(view)
}
Expand Down

0 comments on commit 039d599

Please sign in to comment.