autorelease

Why is release often called shortly after a local var is used instead of just autoreleasing

一个人想着一个人 提交于 2019-12-23 23:25:06
问题 I often see something like the following: UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)]; self.navigationItem.rightBarButtonItem = anotherButton; [anotherButton release]; specifically where a local var, in this case 'anotherButton' is used and then later released. Is this exactly the same as autoreleasing it when it is alloc'ed and then making sure it is used before the method

Objective-C - weak object is registered in autoreleasepool automatically?

自闭症网瘾萝莉.ら 提交于 2019-12-23 17:22:33
问题 I am reading Pro Multithreading and Memory Management for iOS and OS X with ARC, Grand Central Dispatch, and Blocks written by Kazuki Sakamoto. The book wrote: When a variable with a __weak qualifier is used, the object is always registered in autoreleasepool. id __weak obj1 = obj0; NSLog(@"class=%@", [obj1 class]); The above source code is equivalent to: id __weak obj1 = obj0; id __autoreleasing tmp = obj1; NSLog(@"class=%@", [tmp class]); Why does the object need to be registered in

iPhone - Objective-C Memory Leak with SBJsonParser

柔情痞子 提交于 2019-12-23 03:16:14
问题 I keep getting the following memory leak using the "Leaks" tool in Xcode. As this is a library, I'm just wondering what would be the best way to fix such a leak. Any help would be greatly appreciated. I am happy to share more code if needed. UPDATE: I found this article, which doesn't seem promising. Has anyone got any suggestions as to how to fix this? http://code.google.com/p/json-framework/issues/detail?id=13 This is how I'm using the library. - (void)getFacebookProfileFinished:

Objective-C initialize (static method) called more that once?

那年仲夏 提交于 2019-12-21 12:08:51
问题 I have code similar to this in Objective-C: SubclassOfNSObject *GlobalVariableThatShouldNeverChange; @implementation MyClass +(void) initialize { [super initialize]; GlobalVariableThatShouldNeverChange = [[SubclassOfNSObject alloc] init]; // Change more stuff with GlobalVariableThatShouldNeverChange } @end I have this referenced throughout code, and the pointer to this should never change because I am using it everywhere through my code. The problem is, that when I run my tests using GHUnit ,

Objective-C autorelease pool not releasing object

做~自己de王妃 提交于 2019-12-20 02:46:31
问题 I am very new to Objective-C and was reading through memory management. I was trying to play around a bit with the NSAutoreleasePool but somehow it wont release my object. I have a class with a setter and getter which basically sets a NSString *name. After releasing the pool I tried to NSLog the object and it still works but I guess it should not? @interface TestClass : NSObject { NSString *name; } - (void) setName: (NSString *) string; - (NSString *) name; @end @implementation TestClass -

Autorelease vs. release

不羁的心 提交于 2019-12-18 13:37:11
问题 When I need an array for temporary use, what's the difference between these: 1: NSMutableArray *stuff = [[NSMutableArray alloc] init]; // use the array [stuff release]; 2: NSMutableArray *stuff = [NSMutableArray array]; // use the array 3: NSMutableArray *stuff = [[[NSMutableArray alloc] init] autorelease]; // use the array I prefer number 2, since it's shorter. Are there any good reasons to use number 1 or 3? 回答1: Number 2 is likely the best choice in most cases. Number 1 has the chance of

Why is there no autorelease pool when I do performSelectorInBackground:?

ぃ、小莉子 提交于 2019-12-18 12:47:12
问题 I am calling a method that goes in a background thread: [self performSelectorInBackground:@selector(loadViewControllerWithIndex:) withObject:[NSNumber numberWithInt:viewControllerIndex]]; then, I have this method implementation that gets called by the selector: - (void) loadViewControllerWithIndex:(NSNumber *)indexNumberObj { NSAutoreleasePool *arPool = [[NSAutoreleasePool alloc] init]; NSInteger vcIndex = [indexNumberObj intValue]; Class c; UIViewController *controller = [viewControllers

When does autorelease actually cause a release in Cocoa Touch?

做~自己de王妃 提交于 2019-12-18 12:36:31
问题 I understand you need to be careful with autorelease on iOS. I have a method that is returning an object it alloc s which is needed by the caller, so in this situation -- as I understand it -- I need to send autorelease to the object in the callee before it returns. This is fine, but once control returns to the phone (i.e. after my button click has been processed) it seems that the autorelease pool is released. I suspect this is how it is supposed to be, but I am wondering what is the best

double free*** set a breakpoint in malloc_error_break to debug in ARC

南笙酒味 提交于 2019-12-17 21:05:34
问题 I am using ARC in my application with core data and threading etc, after doing all that hard work to get core data work with threading without any crashes now I am getting a new crash with the reason- double free*** set a breakpoint in malloc_error_break to debug How am I supposed to handle this? I have no control on the objects' retain counts. 回答1: Enable Zombie Follow this link : http://iphone2020.wordpress.com/2012/02/23/capturing-exceptions-while-debugging/. This will suggest exact point

Use autorelease before adding objects to a collection?

谁都会走 提交于 2019-12-17 18:59:57
问题 I have been looking through the questions asked on StackOverflow, but there are so many about memory management in Objective-C that I couldn't find the answer I was looking for. The question is if it is ok (and recommnded) to call autorelease before adding a newly created object to a collection (like NSMutableArray)? Or should I release it explicitly after adding it. (I know NSMutableArray willl retain the object) This illustrates my question: Scenario A (autorelease): - (void) add { // array