Question

I am new to Objective-C and have come by 2 problems of the same sort already when freeing memory. Here is:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]intit];
//^^ NSAutoreleasePool is unavailable: not available in automatic reference counting

[lord release];
//^^ Same error as NSAutoreleasePool

I am not sure why this is not working, it seems to work for others. Anyway if I could get some help on this that would be fantastic, thanks so much!

Was it helpful?

Solution

You cannot use retain/release/autorelease selectors manually when you use Automatic Reference Counting. Manual reference counting was the old way of memory management - now, you should always use ARC and forget manually sending 'release' messages, because they're inserted automatically by the compiler.

NSAutoreleasePool was replaced w/ the language-level construct @autoreleasepool: https://developer.apple.com/library/ios/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html

Edit: @autoreleasepool example:

Here you have 10000 objects in the memory until the parent autoreleasepool drains:

for(int i = 0; i < 10000; i++){
    NSString * s = [NSString alloc] initWithFormat:@"%d",i];
}

At its peak memory usage, this algorithm has 10000 NSStrings in the memory. However, consider the following variant:

for(int i = 0; i < 10000; i++){
    @autoreleasepool{
        NSString * s = [NSString alloc] initWithFormat:@"%d",i];
    }
}

This way, there's only one NSString at a time, which gets deallocated at the end of each iteration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top