automatic-ref-counting

When and why would I want to declare a local variable as __weak using ARC?

喜你入骨 提交于 2019-12-04 07:57:57
Mike Ash has written this introduction to ARC where he introduces something like: __weak Foo *_weakFoo = [object foo]; Why would I want to do that for a local, temporary variable? __weak is a zeroing reference which will set the _weakFoo pointer automatically to nil as soon as the referenced object gets deallocated. Also, __weak is only available in iOS >= 5. When would I run into trouble when I simply do this?: Foo *_weakFoo = [object foo]; This is always expected to return an object or nil. My guess is this: Foo *_weakFoo = [object foo]; [self doSomethingStupid]; // does something bad so foo

Fix potential memory leak in ARC

瘦欲@ 提交于 2019-12-04 07:47:55
The following singleton class (SharedManager) helper method might be causing a retain cycle. Getting warnings in static analyzer: "Potential leak of an object allocated at line ..." How can I fix? I did try making ivar uuid __weak but warning still appears when I analyze. NSString *__weak uuid = (__bridge NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject); Thanks Being called in the class like so: myUUID = [SharedManager generateUUID]; + (NSString *)generateUUID { CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault); NSString *uuid = (__bridge NSString *)CFUUIDCreateString

What does “Receiver type 'CALayer' for instance message is a forward declaration” mean here?

强颜欢笑 提交于 2019-12-04 07:46:22
问题 I'm porting a block of code from an iOS4 project to iOS5 and I'm having some troubles with ARC. The code generates a PDF from a screen capture. PDF Generation Code UIView *captureView; ... NSMutableData *pdfData = [NSMutableData data]; UIGraphicsBeginPDFContextToData(pdfData, captureView.bounds, nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [captureView.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext(); The renderInContext line

beginSheet: block alternative with ARC?

独自空忆成欢 提交于 2019-12-04 07:27:18
Mike Ash created an example of using blocks to handle callbacks from sheets, which seems very nice. This was in turn updated to work with garbage collection by user Enchilada in another SO question at beginSheet: block alternative? , see below. @implementation NSApplication (SheetAdditions) - (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow didEndBlock:(void (^)(NSInteger returnCode))block { [self beginSheet:sheet modalForWindow:docWindow modalDelegate:self didEndSelector:@selector(my_blockSheetDidEnd:returnCode:contextInfo:) contextInfo:Block_copy(block)]; } - (void)my

Delegate assignment causes EXC_BAD_ACCESS

妖精的绣舞 提交于 2019-12-04 06:52:27
问题 I am trying to create a delegate for an NSTextField in my view controller, but the program crashes with EXC_BAD_ACCESS . Why does this happen? I read that I am calling a non-existent object, but I don´t know what does not exist. I am using ARC. This is how the delegate object is created in my view controller: #import <Cocoa/Cocoa.h> #import "Delegate.h" @interface ViewController : NSViewController <NSTextFieldDelegate>{ } @end -- #import "ViewController.h" @implementation ViewController -

ARC instance variable retain release

∥☆過路亽.° 提交于 2019-12-04 06:30:16
问题 If we have .h file like this: @interface TestViewController : UIViewController { __weak NSObject *object; } @end and methods in .m file like this: - (void)viewDidLoad { [super viewDidLoad]; NSObject *localObject = [[NSObject alloc] init]; NSLog(@"%ld", CFGetRetainCount((__bridge CFTypeRef)localObject)); object = localObject; NSLog(@"%ld", CFGetRetainCount((__bridge CFTypeRef)object)); NSLog(@"%ld", CFGetRetainCount((__bridge CFTypeRef)localObject)); } Then we get the following output for

week reference to autoreleased object not getting deallocated in case of NSString

帅比萌擦擦* 提交于 2019-12-04 04:51:48
问题 Why temp object is not released and set to nil even though it is declared as __week . But in case of Person object its working as expected. Do NSString objects memory life cycle is handled differently? How? @interface Person : NSObject @property(nonatomic, strong) NSString *name; - (instancetype)initWithName:(NSString *)name; + (Person *)personWithName:(NSString *)name; @end @implementation Person - (instancetype)initWithName:(NSString *)name { if (self = [super init]) { self.name = name; }

How to ignore “No visible @interface for X declares the selector”?

风格不统一 提交于 2019-12-04 04:39:52
Before ARC, I had an "X may not respond to xxx" warning , which is a pretty harmless warning which does not prevent it from compiling. Now, I am trying to convert my project to ARC, and I have an "No visible @interface for X declares the selector xxx" error , which prevents it from compiling. I know exactly what I am doing, and why the warning was there, and I can tell you that the program is correct. Previously, the compiler compiled it with no problem, and should not now stop it from compiling. It is true that class X's interface does not declare that selector, but X is a class that

[UINavigationController retain]: message sent to deallocated instance

最后都变了- 提交于 2019-12-04 04:29:20
问题 My application crashes when simulating Memory warning in simulator with error: [UINavigationController retain]: message sent to deallocated instance I'm using ARC. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; _window = window; [self startWithFlash]; return YES; } - (void)startWithFlash { [self.window.subviews makeObjectsPerformSelector:@selector

Alternative to Objective-C objects in structs (ARC)

。_饼干妹妹 提交于 2019-12-04 04:24:33
问题 I have the following code here that won't run on ARC since it combines Objective-C objects in structs: struct SingleToManyRelation { id singleObject; NSSet* manyObjects; } I know this is reminiscent of Core Data, but that's not the point ;) I am just looking for a solution to implement something like that without having to create a "container" class. Thanks in advance for your advices, Christian 回答1: Give your objects the __unsafe_unretained attribute and ARC will stop complaining (but keep