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

Add autoreleasepool_leaking #112

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
55 changes: 45 additions & 10 deletions objc2/src/rc/autorelease.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::cell::UnsafeCell;
use core::ffi::c_void;
use core::fmt;
use core::marker::PhantomData;
use core::mem::ManuallyDrop;
use core::{cell::UnsafeCell, ptr};
#[cfg(all(debug_assertions, not(feature = "unstable_autoreleasesafe")))]
use std::{cell::RefCell, thread_local, vec::Vec};

Expand Down Expand Up @@ -72,6 +73,13 @@ impl AutoreleasePool {
}
}

const unsafe fn new_leaking() -> Self {
Self {
context: ptr::null_mut(),
p: PhantomData,
}
}

/// This will be removed in a future version.
#[cfg_attr(
not(all(debug_assertions, not(feature = "unstable_autoreleasesafe"))),
Expand All @@ -80,13 +88,15 @@ impl AutoreleasePool {
#[doc(hidden)]
pub fn __verify_is_inner(&self) {
#[cfg(all(debug_assertions, not(feature = "unstable_autoreleasesafe")))]
POOLS.with(|c| {
assert_eq!(
c.borrow().last(),
Some(&self.context),
"Tried to use lifetime from pool that was not innermost"
)
});
if !self.context.is_null() {
POOLS.with(|c| {
assert_eq!(
c.borrow().last(),
Some(&self.context),
"Tried to use lifetime from pool that was not innermost"
)
});
}
}

/// Returns a shared reference to the given autoreleased pointer object.
Expand Down Expand Up @@ -310,9 +320,21 @@ where
f(&pool)
}

#[cfg(all(test, feature = "unstable_autoreleasesafe"))]
/// TODO
pub fn autoreleasepool_leaking<T, F>(f: F) -> T
where
for<'p> F: FnOnce(&'p AutoreleasePool) -> T + AutoreleaseSafe,
{
let pool = ManuallyDrop::new(unsafe { AutoreleasePool::new_leaking() });
f(&pool)
}

#[cfg(test)]
mod tests {
use super::AutoreleaseSafe;
use core::ptr::NonNull;

use super::{autoreleasepool_leaking, AutoreleaseSafe};
use crate::rc::{Id, Owned};
use crate::runtime::Object;

fn requires_autoreleasesafe<T: AutoreleaseSafe>() {}
Expand All @@ -323,4 +345,17 @@ mod tests {
requires_autoreleasesafe::<*mut Object>();
requires_autoreleasesafe::<&mut Object>();
}

#[test]
fn test_autoreleasepool_leaking() {
let cls = class!(NSObject);
let obj: Id<Object, Owned> = unsafe {
let obj: *mut Object = msg_send![cls, alloc];
let obj: *mut Object = msg_send![obj, init];
Id::new(NonNull::new_unchecked(obj))
};
autoreleasepool_leaking(|pool| {
let _ = obj.autorelease(pool);
});
}
}
4 changes: 3 additions & 1 deletion objc2/src/rc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ mod id_traits;
mod ownership;
mod weak_id;

pub use self::autorelease::{autoreleasepool, AutoreleasePool, AutoreleaseSafe};
pub use self::autorelease::{
autoreleasepool, autoreleasepool_leaking, AutoreleasePool, AutoreleaseSafe,
};
pub use self::id::Id;
pub use self::id_traits::{DefaultId, SliceId, SliceIdMut};
pub use self::ownership::{Owned, Ownership, Shared};
Expand Down