• 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
Re: Understanding ARC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Understanding ARC


  • Subject: Re: Understanding ARC
  • From: Jamie Ojomoh <email@hidden>
  • Date: Sun, 25 May 2014 10:07:53 +0100

Thank you for all your helps.

>>You allocated returnString using an alloc/init sequence so it’s not in an
autorelease pool at all.

So if I use alloc/init then autoreleasepool doesn't work?  If thats so, how
do I use NSMutableString?  I can use NSString without alloc/init, but
Mutable always requires alloc/init or it won't work, will it?  I can't just
initialize like this, can I:

NSMutableString* returnString = @"";

>>In general ARC understands that the return value needs to keep a
reference, so it’s safe to put the return statement inside the autorelease
block. In your specific example, returnString was allocated before you
created your autorelease pool, so that pool won’t release it anyway.

So my example must be:

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

Or don't I need autoreleasepool at all?

Is there any difference in memory releasing / ARC between a class and a
instance method?

And 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?  Is this not something
that can be asked here?

Thank you for the helps again.

Yours faithfully,

Jamie Ojomoh


On Sat, May 24, 2014 at 11:04 PM, Jens Alfke <email@hidden> wrote:

>
> On May 24, 2014, at 2:34 PM, Jamie Ojomoh <email@hidden> wrote:
>
> 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.
>
>
> No, in general ARC understands that the return value needs to keep a
> reference, so it’s safe to put the return statement inside the autorelease
> block. In your specific example, returnString was allocated before you
> created your autorelease pool, so that pool won’t release it anyway. Also,
> you allocated returnString using an alloc/init sequence so it’s not in an
> autorelease pool at all.
>
> (In your example I don’t think the autorelease pool is doing anything at
> all, since there are no calls in that block that would cause any object to
> be autoreleased.)
>
> 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?
>
>
>  I believe ARC will take care of autoreleasing it at the point where it’s
> returned.
>
> Apple’s docs specifically recommend that you _don’t_ worry about when
> objects are being retained or released. ARC basically takes care of it for
> you, so most of the time you can pretend that the app is garbage-collected
> and ignore retain/release entirely. Instead, consider where you need strong
> or weak references to objects; this is mostly a concern when declaring
> instance variables.
>
> —Jens
>
_______________________________________________

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: Jens Alfke <email@hidden>
References: 
 >Understanding ARC (From: Jamie Ojomoh <email@hidden>)

  • Prev by Date: Re: string literals and performance
  • Next by Date: NSURL from non-url NSString
  • Previous by thread: Re: Understanding ARC
  • Next by thread: Re: Understanding ARC
  • Index(es):
    • Date
    • Thread