autorelease

Autorelease pools in appkit applications

橙三吉。 提交于 2019-12-04 23:06:35
I'm having difficulties to understand exactly WHEN autorelease pools are created and released in AppKit apps. For example, if I have an ApplicationController class that overrides init, is there an autorelease pool that gets created before it starts and gets drained after it ends? The main thread in an AppKit application runs an NSRunLoop to process events. NSRunLoop creates a new autorelease pool every time it processes a new event (or timer) and drains it once control flow has returned to the NSRunLoop. So in essence, every pass through the run loop has a fresh autorelease pool. 来源: https:/

Why doesn't this crash?

旧巷老猫 提交于 2019-12-04 19:25:51
问题 I'm attempting to narrow down a bug to a minimum reproducible case and found something odd. Consider this code: static NSString *staticString = nil; int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; if (staticString == nil) { staticString = [[NSArray arrayWithObjects:@"1", @"2", @"3", nil] componentsJoinedByString:@","]; } [pool drain]; NSLog(@"static: %@", staticString); return 0; } I'm expecting this code to crash. Instead, it logs: 2011

[myArray addObject:[[objcBlock copy] autorelease]] crashes on dealloc'ing the array

时光毁灭记忆、已成空白 提交于 2019-12-04 10:33:39
I wrote a class for declaratively describing a sequence of UIView animations. My method takes a vararg of animation blocks and puts them in an array. So in my loop I want to do this: [animations addObject:[[block copy] autorelease]]; I first copy the block so that it is moved to the heap, allowing it to be retain 'ed by the array. Then I autorelease it to relinquish ownership (because the array retains it). However this crashes when the animations array is dealloc'd. (My understanding is that the referenced blocks have already been dealloc'd.) Strange thing is, this works: [animations

Objective-C - weak property - getter autoreleases (Automatic Reference Counting)

删除回忆录丶 提交于 2019-12-04 10:16:46
I have a doubt regarding weak property in ARC (auto reference counting) My understanding (correct me if I am wrong): weak property behaves similar to the assign property except that when the instance that the property was pointing to gets destroyed, the ivar is made to point to nil. Question: I just feel that the getter of the weak property retains and autoreleases. Isn't it suppose to behave like getter of the assign property where the getter doesn't retain and autorelease ?(pls refer to the program) Program: I have given below the program with the actual output and my expected output. Note -

NSURLCache crashes with autoreleased objects, but leaks otherwise

余生长醉 提交于 2019-12-04 08:58:58
CSURLCache is designed to cache resources for offline browsing, as NSURLCache only stores data in-memory. If cachedResponse is autoreleased before returning the application crashes, if not, the objects are simply leaked. Any light that could be shed onto this would be much appreciated. Please note stringByEncodingURLEntities is a category method on NSString . @interface CSURLCache : NSURLCache {} @end @implementation CSURLCache - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request { NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,

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

你。 提交于 2019-12-04 05:09:58
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 , I have odd problems with the GlobalVariableThatShouldNeverChange 's pointer being changed (i.e. It is

ARC equivalent of autorelease?

痴心易碎 提交于 2019-12-03 23:31:53
If I have this code, + (MyCustomClass*) myCustomClass { return [[[MyCustomClass alloc] init] autorelease]; } This code guarantees the returning object is autoreleased. What's the equivalent of this in ARC? There is no equivalent in ARC, as you don't need to do it yourself. it will happen behind the scenes and you are not allowed to do it your self. You simply use - + (MyCustomClass*) myCustomClass { return [[MyCustomClass alloc] init]; } I suggest you to watch the ARC introduction in the 2011 WWDC as it very simple when you get it. Look here: https://developer.apple.com/videos/wwdc/2011/ And

Autorelease pools in Objective-C - release main AutoreleasePool?

我的未来我决定 提交于 2019-12-03 15:01:03
By my understanding, when an object is sent an autorelease message, if no autorelease pools exist other than the one in main.m , the object gets placed in the one in main.m . Assuming this is correct, I have a couple of questions: 1) Do all autoreleased objects stay in that pool until the termination of the app? 2) If 1 is true, does creating an autoreleased object without a local autorelease pool (therefore placing that object in the main.m pool) keep that object in memory until termination of the app or a memory warning is received? 3) When is the main.m autorelease pool drained, other than

Why doesn't this crash?

不打扰是莪最后的温柔 提交于 2019-12-03 12:18:32
I'm attempting to narrow down a bug to a minimum reproducible case and found something odd. Consider this code: static NSString *staticString = nil; int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; if (staticString == nil) { staticString = [[NSArray arrayWithObjects:@"1", @"2", @"3", nil] componentsJoinedByString:@","]; } [pool drain]; NSLog(@"static: %@", staticString); return 0; } I'm expecting this code to crash. Instead, it logs: 2011-01-18 14:41:06.311 EmptyFoundation[61419:a0f] static: static: However, if I change the NSLog() to:

iOS autorelease pool blocks

感情迁移 提交于 2019-12-03 08:00:21
问题 I was reading the documentation from apple about memory management when I got to autorelease pool blocks and something got me thinking. Any object sent an autorelease message inside the autorelease pool block is released at the end of the block. I am not sure I fully understand this. Any object created inside an autorelease pool block gets released at the end of the block anyway because that is it's life span. Why would you need to call autorelease to the object when it is going to get