Skip to content

Commit b637c96

Browse files
committed
Add test for sending message to a wrapped object
This appears to leak memory, but it is a idiom that is used in quite many places. To observe the leak, try running cargo test --no-run && leaks -atExit -- ./target/debug/objc-c36d5ae3a7514045 To see it disappear, comment out the line that does `msg_send![obj, isEqual:obj];` in the test.
1 parent 39a6d52 commit b637c96

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/message/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,11 @@ mod tests {
293293
// Unimplemented selector
294294
assert!(obj.verify_message::<(u32,), ()>(sel!(setFoo)).is_err());
295295
}
296+
297+
#[test]
298+
fn test_send_message_wrapped_object() {
299+
let obj = test_utils::WrappedObject::new();
300+
unsafe { msg_send![obj, isEqual:obj]; }
301+
}
302+
296303
}

src/test_utils.rs

+34
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,37 @@ pub fn custom_subclass() -> &'static Class {
177177
pub fn custom_subclass_object() -> CustomObject {
178178
CustomObject::new(custom_subclass())
179179
}
180+
181+
pub struct WrappedObject(std::ptr::NonNull<crate::runtime::Object>);
182+
pub enum WrappedObjectRef {}
183+
impl WrappedObject {
184+
pub fn new() -> Self {
185+
let cls = class!(NSObject);
186+
unsafe {
187+
let obj: *mut crate::runtime::Object = msg_send![cls, alloc];
188+
let res: Self = msg_send![obj, init];
189+
res
190+
}
191+
}
192+
}
193+
194+
unsafe impl crate::Message for WrappedObject {}
195+
unsafe impl crate::Message for WrappedObjectRef {}
196+
197+
impl Deref for WrappedObject {
198+
type Target = WrappedObjectRef;
199+
200+
fn deref(&self) -> &WrappedObjectRef {
201+
let obj_ptr = self.0.as_ptr();
202+
let ref_ptr = obj_ptr as *const WrappedObjectRef;
203+
unsafe { &*ref_ptr }
204+
}
205+
}
206+
207+
impl Drop for WrappedObject {
208+
fn drop(&mut self) {
209+
unsafe {
210+
msg_send![self.0.as_ptr(), release];
211+
}
212+
}
213+
}

0 commit comments

Comments
 (0)