| allocated_partial_init.rs |
|
14130 |
| autorelease.rs |
|
22212 |
| mod.rs |
# Reference counting utilities.
The types in this module provide roughly the same benefits as ARC
(Automatic Reference Counting) does to Objective-C.
Most importantly, a smart pointer [`Retained`] is provided to ensure that
objects are correctly retained and released when created and dropped,
respectively.
Weak references may be created using the [`Weak`] struct; these will not
retain the object, but one can attempt to load them and obtain an `Retained`, or
safely fail if the object has been deallocated.
See [the clang documentation][clang-arc] and [the Apple article on memory
management][mem-mgmt] (similar document exists [for Core Foundation][cf])
for more information on automatic and manual reference counting.
It can also be useful to [enable Malloc Debugging][mem-debug] if you're trying
to figure out if/where your application has memory errors and leaks.
[clang-arc]: https://clang.llvm.org/docs/AutomaticReferenceCounting.html
[mem-mgmt]: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html
[cf]: https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/CFMemoryMgmt.html
[mem-debug]: https://developer.apple.com/library/archive/documentation/Performance/Conceptual/ManagingMemory/Articles/MallocDebug.html
## Example
```
use objc2::rc::{autoreleasepool, Retained, Weak};
use objc2::runtime::NSObject;
// Allocate and initialize a new `NSObject`.
// `Retained` will release the object when dropped.
let obj: Retained<NSObject> = NSObject::new();
// Cloning retains the object an additional time
let cloned = obj.clone();
autoreleasepool(|pool| {
// Autorelease consumes the Retained, but won't actually
// release it until the end of the autoreleasepool
// SAFETY: The given is the innermost pool.
let obj_ref: &NSObject = unsafe { Retained::autorelease(cloned, pool) };
});
// Weak references won't retain the object
let weak = Weak::from_retained(&obj);
drop(obj);
assert!(weak.load().is_none());
``` |
2993 |
| retained.rs |
|
39503 |
| retained_forwarding_impls.rs |
Trivial forwarding impls on `Retained`-like types.
Kept here to keep `retained.rs` free from this boilerplate, and to allow
re-use in `objc2-core-foundation` and `dispatch2` (this file is symlinked
there as well). |
10145 |
| retained_traits.rs |
Helper traits for Retained. |
5824 |
| test_object.rs |
Note: This file is included in the `tests` crate viaa symlink as well. |
10480 |
| weak.rs |
|
8516 |