Igor Delovski Board Forum Index Igor Delovski Board
My Own Personal Slashdot!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ARC

 
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Mac Cocoa
Mac Cocoa  
Author Message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3136
Location: Europe

PostPosted: Sun Jan 13, 2019 2:10 pm    Post subject: ARC Reply with quote

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
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3136
Location: Europe

PostPosted: Sun Mar 05, 2023 7:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3136
Location: Europe

PostPosted: Wed Jun 14, 2023 7:26 pm    Post subject: Reply with quote

so NSObject: retained, removed from array and returned with autorelease, what to do for ARC?

"I have the following piece of code:"

Code:
NSObject *anObject = [[objArray lastObject] retain];
[objArray removeLastObject];
return [anObject autorelease];


"With ARC I cannot use retain or autorelease"
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3136
Location: Europe

PostPosted: Wed Sep 06, 2023 5:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3136
Location: Europe

PostPosted: Wed Sep 06, 2023 5:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3136
Location: Europe

PostPosted: Wed Sep 06, 2023 5:18 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 3136
Location: Europe

PostPosted: Wed Sep 06, 2023 6:03 pm    Post subject: Reply with quote

so - What is "CFRetain+CFAutorelease" behavior in iOS under ARC?

"CFAutoRelease is useful for returning CFObjects as function results that the
caller doesn't have to release himself/herself."
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Mac Cocoa All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Delovski.hr
Powered by php-B.B. © 2001, 2005 php-B.B. Group