automatic-ref-counting

Am I correctly creating and passing this C array to Objective-C method and referencing it with a property?

你离开我真会死。 提交于 2019-12-02 10:20:57
I created a C array like this: unsigned char colorComps[] = {2, 3, 22, 55, 9, 1}; which I want to pass to an initializer of an Objective-C object. So I think I have to put the array on the heap: size_t arrayByteSize = numColorCompVals * sizeof(unsigned char); unsigned char *colorCompsHeap = (unsigned char*)malloc(arrayByteSize); Then I have to write my first "stack memory array" to the heap array in for loop: for (int i = 0; i < numColorCompVals; i++) { colorCompsHeap[i] = colorComps[i]; } Side question: Is there a more elegant solution to avoid the for-loop step? And then I pass it to the

ARC with blocks and retain cycles

大憨熊 提交于 2019-12-02 08:56:34
问题 I have a class which holds a block as an object property: @property (nonatomic, readwrite, copy) SFFailureBlock failureBlock; where SFFailureBlock: typedef void (^SFFailureBlock)(NSError *error); I have an operation also declared as an object property (AFHTTPRequestOperation) and I want it to call the failure block once it is completed. [self.operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { __weak NSError *error = [NSError errorWithDomain:@"com

Potential reference count issues unless grabbing fresh reference in background thread

爱⌒轻易说出口 提交于 2019-12-02 07:36:55
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] persistentStoreCoordinator]; // ... }); It also contains this explanation: The reason we grab a fresh reference to the

ARC with blocks and retain cycles

时光毁灭记忆、已成空白 提交于 2019-12-02 07:12:18
I have a class which holds a block as an object property: @property (nonatomic, readwrite, copy) SFFailureBlock failureBlock; where SFFailureBlock: typedef void (^SFFailureBlock)(NSError *error); I have an operation also declared as an object property (AFHTTPRequestOperation) and I want it to call the failure block once it is completed. [self.operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { __weak NSError *error = [NSError errorWithDomain:@"com.test" code:100 userInfo:@{@"description": @"zero results"}]; failureBlock(error); } failure:^

UINavigation pushing a new root controller

ⅰ亾dé卋堺 提交于 2019-12-02 06:06:53
I am trying to push a new root controller to a navigation stack, but using a side reveal menu. My app delegate has the following: welcomeViewController = [[MyWelcomeViewController alloc] initWithNibName:@"MyWelcomeViewController" bundle:nil]; navController = [[UINavigationController alloc] initWithRootViewController:welcomeViewController]; navController.navigationBarHidden = YES; // Then we setup the reveal side view controller with the root view controller as the navigation controller self.revealSideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:navController]

Lack of autorelease optimization under ARC compiler

*爱你&永不变心* 提交于 2019-12-02 04:16:19
I was just wondering, why is there no autorelease pool optimization under the ARC compiler, where it would retain an object in the innermost scope, remove it from the autorelease pool and release once the object is no longer in use? To quote a very impractical example from another question, for(NSUInteger i = 0; i < 10000; i++) { for(NSUInteger j = 0; j < 10000; j++) { NSNumber* n = [NSNumber numberWithUnsignedInteger:j]; //NSLog(@"%@", n); //Disabled this to increase memory bloat faster. } } Without @autoreleasepool { ... } wrapping, memory grows and grows. Wrapping with @autoreleasepool ,

iOS 5 blocks crash only with Release Build

邮差的信 提交于 2019-12-02 03:16:39
I have using blocks and ARC, and found in some situation, iOS only crash in Release build. It was wrong way to write code, like this. -(IBAction)clickedButtonA:(UIBarButtonItem*)sender event:(UIEvent*)event { NSMutableArray *arrRows = [NSMutableArray arrayWithCapacity:0]; #warning this code only crash on Release Build.... Don't use this NSMutableDictionary * dicRow = [NSMutableDictionary dictionaryWithCapacity:0]; [arrRows addObject:dicRow]; dispatch_block_t block = ^{ NSString *str = [NSString stringWithFormat:@"%@",[_tweet valueForKey:@"text"]]; [[UIPasteboard generalPasteboard] setString

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

本秂侑毒 提交于 2019-12-02 00:15:42
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; } return self; } + (Person *)personWithName:(NSString *)name { return [[self alloc] initWithName: name]; }

UIWebView is not deallocating memory

∥☆過路亽.° 提交于 2019-12-01 22:55:29
问题 I'm having some real trouble trying to recover the memory I've allocated for a UIWebView within my application. I essentially create and present the UIWebView temporarily for the user in a separate ViewController , then remove all references and pop the ViewController from the stack. Despite doing all of this, the memory allocated is never returned, and I get stuck with an extra 10 Mb of memory each time I segue to that ViewController again. Let's see how I'm managing this UIWebView anyways.

[UINavigationController retain]: message sent to deallocated instance

夙愿已清 提交于 2019-12-01 21:20:34
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(removeFromSuperview)]; __weak typeof (self) weakSelf = self; WPRSplashViewController