• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Understanding ARC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Understanding ARC


  • Subject: Understanding ARC
  • From: Jamie Ojomoh <email@hidden>
  • Date: Sat, 24 May 2014 22:34:26 +0100

Dear Sir,

I'm trying to teach myself Objective C and I don't understand ARC very
well.  Please could someone help me.

If I've understood this correctly (silly example):

-(NSString*)stringdoodad
{
    NSMutableString* returnString = [[NSMutableString alloc] init];
    @autoreleasepool
    {
        for (NSUInteger i = 0; i < 10; i++)
        {
            NSString* testString = @"hello";
            [returnString appendString:testString];
        }
    }
    return returnString;
}

In the example, everything inside the autoreleasepool block will be
released as soon as the block ends, so it's necessary to declare the return
value outside the block.

When does returnString get released?  Does it get automatically released as
soon as the method ends and the value has been passed into whatever
container was waiting for it?  Do I need to do anything special in order to
make sure that it is released as soon as it's done its job?

If it gets called by the following method:

-(void)stringDoodadCaller
{
    @autoreleasepool
    {
        NSString* testString = [self stringdoodad];
    }
}

Would returnString be released because stringdoodad is called within an
autoreleasepool?

I don't think I've understood Apple's documentation on this - have I got
this right or wrong?

Also, why is it that memory automatically gets released properly when an
application quits, but when a class is released any memory that hasn't been
freed or released properly hangs around forever?

Yours faithfully,

Jamie Ojomoh
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Understanding ARC
      • From: Jim Geist <email@hidden>
  • Prev by Date: Re: State Restoration with NSDocument architecture
  • Next by Date: Re: Understanding ARC
  • Previous by thread: Re: Unwanted Animations
  • Next by thread: Re: Understanding ARC
  • Index(es):
    • Date
    • Thread