3

I am new to IOS development and I have started to learn objective c to program towards IOS 7. and as I know, it is way easier to code now than it has been before because of the Automatic reference counting. there are a couple of things I do not understand . in MAIN method we have the autoreleasepool block, so my first question is that in order to enable ARC , the code has to be inside this block? if no, then what is the difference between the code that is inside autoreleasepool and the rest those aren't?

my second question is that when I am writing my IPHONE programs , I have bunch of classes and non of those codes are inside "autoreleasepool" , only the code inside the MAIN method.

 int main(int argc, char * argv[])
 {
     @autoreleasepool {
    return UIApplicationMain(argc, argv, nil, 
    NSStringFromClass([HomepwnerAppDelegate class]));
 }
}

so , does this mean that this block somehow magically gets applied to all lines of code inside any other classes of the same program?

My last question is that whether with ARC or without it, if we had a declared pointer variable inside a method, does the object gets released/destroyed when the method returns/exit?

assume we have a method like this :

- (void)doSomething {
 NSMutableArray *allItems = [[NSMutableArray alloc] init];
 NSString *myString = @"sample string";
 [allItems addObject:myString]
 }

then when we call this method and it exits, what would happen to those local variables defined inside the method ? is there any difference in the outcome if we are using ARC or not ? (Object are still in the memory or not)

al gh
  • 508
  • 2
  • 15

3 Answers3

4

Autorelease pools predate ARC by about 15 years. Cocoa uses a reference-counting memory management scheme, where (conceptually, at least) objects are created with a reference count of 1, retain increases it by 1 and release decreases the count by 1, and the object is destroyed when the count gets to 0.

A problem with this scheme is that it makes returning an object kind of awkward, because you can't release the object before you return it — if you did, it might be destroyed before the other method got to use it — but you don't want to require the other method to release the object either. This is where autorelease pools come in. An autorelease pool lets you hand an object to it, and it promises to release the object for you later. Under manual retain/release (the way we used to do things before ARC), you did this by sending autorelease to an object.

OK, so then ARC comes into the picture. The only thing that really changes with ARC is that you aren't the one writing retain, release and autorelease anymore — the compiler inserts them for you. But you still need an autorelease pool for autoreleased object to go into.

As for your second question:

My last question is that whether with ARC or without it, if we had a declared pointer variable inside a method, does the object gets released/destroyed when the method returns/exit?

assume we have a method like this :

- (void)doSomething {
    NSMutableArray *allItems = [[NSMutableArray alloc] init];
    NSString *myString = @"sample string";
    [allItems addObject:myString]
}

then when we call this method and it exits, what would happen to those local variables defined inside the method ? is there any difference in the outcome if we are using ARC or not ?

If you're using ARC, the compiler will release any objects referenced by local variables. If you're not using ARC, you'd need write [allItems release] yourself, because the variable going out of scope does not magically cause the object it references to be released.

Community
  • 1
  • 1
Chuck
  • 234,037
  • 30
  • 302
  • 389
  • Thanks for your response. only one question remains, if ARC does the job , then whats the point of having the "autoreleasepool" anymore? – al gh Dec 13 '13 at 19:36
  • @aligh: ARC inserts calls to retain, release and autorelease so you don't have to, but those methods are still the same as always — you just aren't the one calling them. `autorelease` still puts the object in an autorelease pool even when the compiler is the one writing the call to it, so there still needs to be an autorelease pool available for the autoreleased object to go into. – Chuck Dec 13 '13 at 19:42
  • Thanks very much. Would you mind looking at another question that I asked regarding the UINavigationController property in the following link? http://stackoverflow.com/questions/20572690/navigationcontroller-property-is-declared-in-uinavigationcontroller-h-but-is-a-p – al gh Dec 13 '13 at 19:56
  • @chuck, Hi, I'm having a similar issue (but not identical) in the following link https://stackoverflow.com/questions/59336108/keeping-objectivec-object-valid-outside-the-scope-of-a-function... would you mind take a look ? Thanks ! – Irad K Dec 14 '19 at 18:26
2

new to IOS development

Best not to worry, Automatic means that you mostly concentrate on other things ^)

does this mean that this block somehow magically gets applied to all lines of code inside any other classes of the same program

Yes. You're in main function, so all the code that is executed has to be inside this function - your app will terminate once it ends. Unless you create a separate thread, but it's hard to do that by accident.

the code has to be inside this block

As said above, all of your code on main thread will execute within this block.

what would happen to those local variables defined inside the method

You're guaranteed that they will be destroyed before returning.

ilya n.
  • 18,398
  • 15
  • 71
  • 89
2

in MAIN method we have the autoreleasepool block, so my first question is that in order to enable ARC, the code has to be inside this block? if no, then what is the difference between the code that is inside autoreleasepool and the rest those aren't?

ARC is enabled by corresponding Objective-C compiler setting. If you create a new project in the latest version of Xcode it will be enabled by default.

The @autorelease keyword places code inside the curly brackets into autorelease pool scope. Autorelease pools are used both with ARC and manual memory management.

my second question is that when I am writing my IPHONE programs , I have bunch of classes and non of those codes are inside "autoreleasepool" , only the code inside the MAIN method.

iOS applications are event based. Main thread starts event loop when you call UIApplicationMain function processing touch events, notifications etc. This event loop has its own autorelease pool that autoreleases objects at the end of the loop iteration. This autorelease pool has nothing to do with the autorelease pool you see in main function.

My last question is that whether with ARC or without it, if we had a declared pointer variable inside a method, does the object gets released/destroyed when the method returns/exit?

If you use ARC the objects will be released (unless you return a reference to an object from the method). In MMR you would need to manually send release message to destroy the objects.

yurish
  • 1,515
  • 1
  • 15
  • 16