automatic-ref-counting

ARC - why do object pointers require explicit ownership type in function definitions?

拜拜、爱过 提交于 2019-12-07 16:38:14
问题 void testFunction (id testArgument[]) { return; } I'm getting the error "Must explicitly describe intended ownership of an object array parameter". Why does ARC need me to specify the ownership type of the objects in the testArgument array? 回答1: To expand on Jeremy's answer, ARC had two primary goals when designed: make memory management as fully automatic as possible in pure Objective-C code while also preserving or maximizing efficiency (in fact, ARC can be more efficient than manual retain

Accessor implementation of properties

纵饮孤独 提交于 2019-12-07 15:22:09
问题 Is there some of documentation how the compiler auto-generates the accessors of properties? When writing custom accessors (overriding the synthesized ones), it would be nice to know the original implementation. Especially to see the differing implementations of accessors for properties with different (weak/strong/retain/copy etc..) attributes. 回答1: Is there some of documentation how the compiler auto-generates the accessors of properties? The compiler just adds a C function call. Peek at the

Why __weak object will be added to autorelease pool?

筅森魡賤 提交于 2019-12-07 14:39:21
问题 id __weak obj1 = obj0; equals id __weak obj1 = obj0; id __autoreleasing tmp = obj0; In Pro multithreading and memory management for iOS and OSX . But why the obj1 need to add to the autorelease pool, I think making a weak pointer of an object should not affect its lifetime. 回答1: { NSObject* sp = [NSObject new]; NSObject* __weak wp = sp; } the above code is translate to: id sp = objc_msgSend(NSObject, "new"); id wp; objc_initWeak(&wp, sp); objc_destroyWeak(&wp); objc_storeStrong(&sp, 0); 1)

ARC unavailable methods in Swift

点点圈 提交于 2019-12-07 13:35:32
问题 I was able to see an interesting case using Estimote nearables SDK They have a class ESTNearable with property called zone . // ENUM typedef NS_ENUM(NSInteger, ESTNearableZone ) { ESTNearableZoneUnknown = 0, ESTNearableZoneImmediate, ESTNearableZoneNear, ESTNearableZoneFar, }; // CLASS @interface ESTNearable : NSObject <NSCopying, NSCoding> // ... @property (nonatomic, assign, readonly) ESTNearableZone zone; // ... @end So when I try to use this method in Swift , compiler fails with that

Objective C - Self Zeroing weak pointer unexpected behaviour

走远了吗. 提交于 2019-12-07 13:00:09
问题 I have recently upgraded from Mavericks to Yosemite and now my unit tests are failing. The problem boiled down to a typo in a weak pointer to string content. Please see the following sample code: NSString* value1; NSString* value2; __weak NSString* weakValue1; __weak NSString* weakValue2; NSMutableString* resultText = [NSMutableString new]; @autoreleasepool { value1 = [NSString stringWithFormat: @"Hello: %d", 1]; value2 = [NSString stringWithFormat: @"Hello %d", 2]; weakValue1 = value1;

ARC on older versions of iOS and OS X

十年热恋 提交于 2019-12-07 12:37:14
问题 This is to clear up some doubt in my conceptual understanding of ARC . If ARC is a compile time technology why isnt it available on all versions of iOS and OS X ? 回答1: ARC isn't just a compile-time technology. It also relies on some runtime components. There's two parts to this: The reference counting. At compile-time, ARC introduces calls to a bunch of helper functions. These are all documented online, but the important ones are objc_retain() , objc_release() , and objc_autorelease() . These

super dealloc on ARC subclasses

不问归期 提交于 2019-12-07 09:00:36
问题 In ARC I'm aware that you do not call [super dealloc] in any overrides of -dealloc , so typically I remove observers and timers in there without doing so. However, if I were to subclass a view that I made that releases observation info in -dealloc without calling [super dealloc] in the subclass' implementation of the method, would the super implementation be called automatically to release the observation info handled by the superclass, or would it leak? 回答1: The superclass' implementation of

ARC error when deploying to 10.6

一曲冷凌霜 提交于 2019-12-07 05:54:13
问题 I have my app running and tested on 10.7 but later realized that I needed it to work on earlier versions as well. And unfortunately, I started this project with ARC on from the very beginning. Now my build setup is debug base sdk: 10.7, and release base sdk: 10.6. And when I try to archive my app, I got the following error: Undefined symbols for architecture x86_64: "_objc_retain", referenced from: +[__ARCLite__ load] in libarclite_macosx.a(arclite.o) -[AppDelegate

ARC & Malloc: EXEC_BAD_ACCESS

放肆的年华 提交于 2019-12-07 05:42:26
问题 I have been working on a project for some time now, and I decided to make the jump to ARC. I came across some code that was bombing out every time, and I would like to know why. I have managed to simplify it down to this snippet: typedef __strong id MYID; int main(int argc, char *argv[]) { MYID *arr = (MYID *) malloc(sizeof(MYID) * 4); arr[0] = @"A"; // always get an EXEC_BAD ACCESS HERE arr[1] = @"Test"; arr[2] = @"Array"; arr[3] = @"For"; // uh oh, we need more memory MYID *tmpArray = (MYID

“__block” variable results in nil value when go out of block

两盒软妹~` 提交于 2019-12-07 05:25:36
问题 I wanna use __block variable to get value in block. But when out of block, the __block variable seems to be nil. Why this would happen? NSString *fileName = [Tools MD5Encode:url]; __block NSString *filePath = nil; [fileList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSString *aFileName = obj; if ([aFileName isEqualToString:fileName]) { NSString *path = [VERSIONS_INFO_DATA_DIRECTORY stringByAppendingPathComponent:aFileName]; filePath = path; NSLog(@"filePath1 %@",