Mac Cocoa |
Author |
Message |
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Sun Jan 13, 2019 2:10 pm Post subject: ARC |
|
|
so - How to force release on iOS
"A bit more general answer, I found how you can force release an object:"
Code: | #import <objc/message.h>
// ---
while ([[object valueForKey:@"retainCount"] integerValue] > 1) {
objc_msgSend(object, NSSelectorFromString(@"release"));
}
objc_msgSend(object, NSSelectorFromString(@"release")); |
"But you shouldn't do this because ARC will probably release the object later
and this will cause a crash. This method should be only used in debug!" |
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Sun Mar 05, 2023 7:13 pm Post subject: |
|
|
so - Breaking retain cycle with strong/weak self
"Any non-weak object that you reference inside the block will result in an
implicit retain on that object, as the block is being created. Not executed,
but created.
The pattern is:"
Code: | __weak typeof(self) weakSelf = self;
[manager someAsynchronousMethodWithCompletionHandler:^{
typeof(self) strongSelf = weakSelf;
if (strongSelf) {
...
}
}];
|
|
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Wed Sep 06, 2023 5:04 pm Post subject: |
|
|
so - Convert method that returns an autoreleased CGColor to ARC
"I'm in the process of converting my project to using ARC. I have a category on
NSColor with a method that returns an autoreleased CGColor representation"
"Various hoops to send it an -autorelease message or equivalent. All of these
are a bit messy because they rely on "fooling" ARC, except the last one
which assumes id is ABI-compatible with void*. They're also probably slower
than the above because they need to look up a class/selector
(objc_lookUpClass() and sel_registerName() might be faster or even optimi-
zed away, but I wouldn't bet on it)."
Code: | return (__bridge CGColorRef)[(__bridge id)theColor performSelector:NSSelectorFromString(@"autorelease")]
[NSClassFromString(@"NSAutoreleasePool") addObject:(__bridge id)theColor]
return theColor;
return (__bridge CGColorRef)((id(*)(id,SEL))objc_msgSend)((__bridge id)theColor,NSSelectorFromString(@"autorelease"));
return ((void*(*)(void*,SEL))objc_msgSend)(theColor,NSSelectorFromString(@"autorelease"));
|
|
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Wed Sep 06, 2023 5:16 pm Post subject: |
|
|
mjtsai.com - Adding ARC Code to a Project That Also Compiles for i386
"ARC can be enabled for individual files with the -fobjc-arc compiler flag. In
Xcode, you can set per-file flags under Build Phases in the Compile Sources
build phase."
Topic: ARC
Last edited by Ike on Wed Sep 06, 2023 6:04 pm; edited 1 time in total |
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
Posted: Wed Sep 06, 2023 5:18 pm Post subject: |
|
|
mjtsai.com - Autoreleasing Core Foundation Objects With ARC
"Since CFAutorelease() requires Mac OS X 10.9, Daniel Jalkut suggests
implementing your own function in a file that's not compiled with ARC:"
Code: | CFTypeRef MAAutorelease(CFTypeRef CF_RELEASES_ARGUMENT arg)
{
return [(id)arg autorelease];
} |
|
|
Back to top |
|
|
Ike Kapetan
Joined: 17 Jun 2006 Posts: 3136 Location: Europe
|
|
Back to top |
|
|
|