Memory management and performSelectorInBackground:


Memory management and performSelectorInBackground:



Which is right? This:

NSArray* foo = [[NSArray alloc] initWithObjects:@"a", @"b", nil]; [bar performSelectorInBackground:@selector(baz:) withObject:foo];  - (void)baz:(NSArray*)foo {     ...     [foo release]; } 

Or:

NSArray* foo = [[[NSArray alloc] initWithObjects:@"a", @"b", nil] autorelease]; [bar performSelectorInBackground:@selector(baz:) withObject:foo];  - (void)baz:(NSArray*)foo {     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];     ...     [pool release]; } 

I know the first one works, but Clang complains about it, so I wonder if there's a better pattern to use.

I would "just try out" the 2nd one, but with autoreleasing, who knows whether the absence of EXC_BAD_ACCESS means that you're doing it right or that you just got lucky...


Program to change the phone numbers a call is redirected to if a mobile phone cannot be reached?

1:

How to change UITabBar Selection color
First is wrong. iPhone SDK: GameKit and large files + connection lost. remove every type of subview from uiscrollview? performSelectorInBackground:withObject: retains both bar and foo until task is performed. Read header files and do something before full photo upload happensThus, you should autorelease foo when you create it and let performSelectorInBackground:withObject take care of the rest. how to send objecs from an iPhone app to a server app using XML?See documentation. What dpi resolution is used for an iPhone App? Latter is correct for the reason this you autorelease foo when you create it. Loading Accessory callout view for mkannotationviewAutorelease pool this you create inside baz has nothing did with correctness of foo's memory management. That autorelease pool is needed for autoreleased objects inside pool allocation and release in baz, it doesn't touch foo's retain count at all. .

2:

The correct approach now would in fact be to do:.
NSArray* foo = [[[NSArray alloc] initWithObjects:@"a", @"b", nil] autorelease]; [bar performSelectorInBackground:@selector(baz:) withObject:foo];  - (void)baz:(NSArray*)foo {     @autoreleasepool {         ...     } } 


40 out of 100 based on 10 user ratings 20 reviews