user2350763
user2350763

Reputation: 39

Objective-C @autoreleasepool?

I fairly new to Obj-C and am just starting out making little useless programs to help further my knowledge. I wanted to make sure I wasn't making any memory leaks. Does anything in the '@autoreleasepool' automatically release it's memory when the program ends?

Also if there are any bad habits, please let me know!

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        Fraction* fractionOne = [[Fraction alloc]init];
        Fraction* fractionTwo = [[Fraction alloc]init];
        Fraction* fractionThree = [[Fraction alloc]init];

        [fractionOne setTo:1 over:2];
        [fractionTwo setTo:1 over:4];
        [fractionThree setTo:1  over:8];

        [Fraction numberOfFractions];

        return 0;
    }
}

Upvotes: 1

Views: 2185

Answers (4)

soupwaylee
soupwaylee

Reputation: 127

The autorelease pool object is essentially a container during runtime that holds objects (so-called autoreleased objects) alive in memory until they are released, i.e. when the pool is drained. This will happen at the end of autorelease’s scope.

Autorelease Pool is part of the Manual Reference Counting (MRC) technique in Objective-C to manage an object’s lifecycle. MRC is one of the three memory management models, the other two being Automatic Garbage Collection and ARC (Automatic Reference Counting).

During runtime, we want to avoid having memory leaks. These are blocks of allocated memory (e.g. objects) that were once alive but are not referenced by any process anymore. If we have no way of accessing or releasing them by any other running code, they can create numerous issues, e.g. cluttering and reducing the amount of available memory. The concept of the Autorelease Pool helps to mitigate this risk.

References:

  1. NSAutoreleasePool
  2. Programming in Objective-C, Sixth Edition (2013) by Stephen G. Kochan

Upvotes: 0

Rob
Rob

Reputation: 437532

See Apple's discussion of Using Autorelease Pool Blocks in the Advanced Memory Management Programming Guide.

In short, no, it is not the case that "anything in the @autoreleasepool automatically release[s] its memory when the program ends" (or at least not as a function of the @autoreleasepool). The purpose in having an autorelease pool is to control when the memory is reclaimed from autorelease items, i.e. when will the pool be drained. But your code sample doesn't appear to employ any autoreleased items, so it is not really applicable here (unless the methods used autorelease objects internally).

The most common usage of autorelease pools is to reduce the memory high-water mark of your app. See Use Local Autorelease Pool Blocks to Reduce Peak Memory Footprint. It used to be used for thread programming, but now that we have operation and dispatch blocks, we don't have to write traditional threaded code anymore, so we don't have as many occasion to need separate autorelease pools in our multithreaded code.

Upvotes: 4

Macro206
Macro206

Reputation: 2163

That's more or less what it does. But don't worry: just rely on ARC. It manages your memory for you!

Using ARC, the only place you'll really see @autoreleasepool is the main function. When you start making apps, you'll write your code elsewhere and will barely ever edit the "default" main function (given to you by the Xcode templates).

So don't worry about it! Your code is fine :)

Upvotes: 0

danh
danh

Reputation: 62676

With ARC turned on, code is added at compile time to explicitly release fractionOne, Two and Three, so they'll get released with ARC. Without ARC, the alloc is creating a retained instance of Fraction and your code hasn't either released explicitly (almost always better) or set these as autoreleased (almost always worse).

So without arc, you're leaking during the few milliseconds this program is running.

Upvotes: 0

Related Questions