automatic-ref-counting

What is the equivalent of @autoreleasepool in Swift?

五迷三道 提交于 2019-12-02 19:53:43
In Swift, I notice there is no @autoreleasepool{} construct, although Swift does use ARC. What is the proper way to manage an autoreleasepool in Swift, or has it been removed for some reason? This is explained in detail in WWDC 2014 session video number 418 "Improving Your App with Instruments", which you can also download as a PDF. But in short, the syntax is: autoreleasepool { /* code */ } Just FYI, Xcode constructed the full code as follows: autoreleasepool({ () -> () in // code }) Guess the parentheses identifies the functions closure. There is! It's just not really mentioned anywhere.

Do loops and convenience methods cause memory peaks with ARC?

偶尔善良 提交于 2019-12-02 19:33:30
I'm working with ARC and seeing some strange behavior when modifying strings in a loop. In my situation, I'm looping using NSXMLParser delegate callbacks, but I see the same exact behavior and symptoms using a demo project and sample code which simply modifies some NSString objects. You can download the demo project from GitHub , just uncomment one of the four method calls in the main view controller's viewDidLoad method to test the different behaviors. For simplicity's sake, here's a simple loop which I've stuck into an empty single-view application. I pasted this code directly into the

What are the new type qualifiers introduced with ARC?

半世苍凉 提交于 2019-12-02 19:14:50
Automatic Reference Counting (ARC) introduces some new type qualifiers. I've seen __strong and __weak , but what do they do? Jacob Relkin __strong means that on assignment, the rvalue of the expression will be retained and stored into the lvalue using primitive semantics. (To deallocate such an object, all you must do is assign it nil , the previously referenced object will be released, nil will be retained, which effectively does nothing and it's peaches and cream.) __unsafe_unretained and __weak are similar in the sense that the address of the rvalue will be assigned to the lvalue, but if

After Auto-ARC Conversion: Assigning retained object to unsafe property; object will be released after assignment

流过昼夜 提交于 2019-12-02 18:54:52
I just converted an old project to ARC using Xcode's automatic refactoring. @property (nonatomic, retain) NSMutableArray *cards; was replaced by: @property (nonatomic) NSMutableArray *cards; This makes sense because what I've read is that "strong" is the default state. However, the following line is giving me the error in the title: self.cards = [[NSMutableArray alloc] initWithCapacity:54]; The error is solved by adding strong back in where retain used to be: @property (nonatomic, strong) NSMutableArray *cards; However... if I need to go back and put strong in to every @property declaration

Need Reachability version for ARC in iOS5

安稳与你 提交于 2019-12-02 18:52:48
Using Apple's Reachability code in iOS5 I get a bunch of compilation errors as shown below. Any ideas on what is happening here? I'm using ARC so I have edited the standard code slightly to remove autorelease/retain and the NSAutoReleasePool . Undefined symbols for architecture armv7: "_SCNetworkReachabilityCreateWithAddress", referenced from: +[Reachability reachabilityWithAddress:] in Reachability.o "_SCNetworkReachabilityCreateWithName", referenced from: +[Reachability reachabilityWithHostName:] in Reachability.o "_SCNetworkReachabilityUnscheduleFromRunLoop", referenced from: -[Reachability

Do I need use dealloc method with ARC?

谁说胖子不能爱 提交于 2019-12-02 18:46:35
So, I have class: @interface Controller : NSObject { UILabel* fileDescription; } @property(strong, nonatomic) UILabel* fileDescription; Do I need use method dealloc where property fileDescription will equal nil? For example: -(void)dealloc { fileDescription = nil; } If not, who will dismiss memory used by fileDescription? Generally you don't need to provide a subclassed dealloc method as ARC manages the lifetime of the instance variables. However it can be useful to perform clean-up other than releasing objects , for example to remove an observer or close a network connection down cleanly. You

How to properly address “Weak receiver may be unpredictably null in ARC mode”

限于喜欢 提交于 2019-12-02 18:41:24
I turned on a new flag in xcode and get the warning "Weak receiver may be unpredictably null in ARC mode". This confuses me because OF COURSE it could be nil. I asked this question a week ago and received no answer, but Greg Parker answered it on the mailing list. So I am reposting with the answer. We added this warning because we saw lots of subtle and hard to debug problems in practice. The recommended practice is to read the weak variable into a strong local variable once, and then use the local variable. Greg Parker In my first incarnation of this question, I posted something like this,

Potential reference count issues unless grabbing fresh reference in background thread

元气小坏坏 提交于 2019-12-02 18:35:57
问题 I have a second question after reading Marcus S. Zarra's (excellent) Core Data: Data Storage and Management for iOS, OS X, and iCloud (2nd edition) if I may. The book's section Asynchronously Adding the NSPersistentStore contains this piece of code (excerpt): dispatch_queue_t queue; queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ // ... NSPersistentStoreCoordinator *coordinator = nil; coordinator = [[self managedObjectContext]

Manual retain with ARC

送分小仙女□ 提交于 2019-12-02 18:07:24
Before ARC I had the following code that retains the delegate while an async operation is in progress: - (void)startAsyncWork { [_delegate retain]; // calls executeAsyncWork asynchronously } - (void)executeAsyncWork { // when finished, calls stopAsyncWork } - (void)stopAsyncWork { [_delegate release]; } What is the equivalent to this pattern with ARC? Why not just assign your delegate object to a strong ivar for the duration of the asynchronous task? Or have a local variable in executeAsyncWork - (void)executeAsyncWork { id localCopy = _delegate; if (localCopy != nil) // since this method is

objective-c ARC readonly properties and private setter implementation

青春壹個敷衍的年華 提交于 2019-12-02 18:03:38
Prior to ARC, if I wanted a property to be readonly to using it but have it writeable within the class, I could do: // Public declaration @interface SomeClass : NSObject @property (nonatomic, retain, readonly) NSString *myProperty; @end // private interface declaration @interface SomeClass() - (void)setMyProperty:(NSString *)newValue; @end @implementation SomeClass - (void)setMyProperty:(NSString *)newValue { if (myProperty != newValue) { [myProperty release]; myProperty = [newValue retain]; } } - (void)doSomethingPrivate { [self setMyProperty:@"some value that only this class can set"]; }