• 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: Autorelease Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Autorelease Question


  • Subject: Re: Autorelease Question
  • From: Filip van der Meeren <email@hidden>
  • Date: Thu, 20 Nov 2008 00:17:07 +0100

I feel the need to express one concern about this topic:

If you already know you will autorelease the object and return it, then you should call the autorelease at the same moment you create the object.

NSString *str = [[[NSString alloc] init] autorelease];

If you wait until the end of your method to autorelease the object, you are leaving a whole range of possible memoryleaks open, since every call you make between the allocation and release is a call that might throw an exception (and we all know Objective-C uses memoryjumps from C).

Filip van der Meeren
http://www.sourceforge.net/projects/xlinterpreter

On 20 Nov 2008, at 00:07, DKJ wrote:

If you've done this:

	NSString *str = [NSString string];

then you can simply do this:

	return str;

Whoever is getting the returned value should retain it, and then release it when they're done.

If you'd done this instead:

	NSString *str = [[NSString alloc] init];

then you should do this:

	return [str autorelease];

Once again, whoever is getting the returned value should retain it, and then release it when they're done.

In general, if you retain an object, or use a either a "copy" or an "init..." method to obtain it, you need to release it yourself when you're done with it.

Otherwise, you can assume that the object will dispose of itself at some future time. But when this will happen is in practice unpredictable, which is why you need to retain the object before doing anything else with it.

dkj
	

On 19-Nov-08, at 14:37 , Carmen Cerino Jr. wrote:

I am not sure the best way to phrase this question into words, so I
will phrase it using example code.

- (NSString*)foo
{

  NSString blah = [NSString string];

  .........


//Now do I do: return blah;

  //Or:
  return [[blah retain] autorelease]];
}
_______________________________________________

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

===================== Hatzic Intellectual Software Victoria BC, Canada




_______________________________________________

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

_______________________________________________

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: Autorelease Question
      • From: DKJ <email@hidden>
    • Re: Autorelease Question
      • From: "Kyle Sluder" <email@hidden>
References: 
 >Autorelease Question (From: "Carmen Cerino Jr." <email@hidden>)
 >Re: Autorelease Question (From: DKJ <email@hidden>)

  • Prev by Date: Re: NSButton ical Like
  • Next by Date: Re: Autorelease Question
  • Previous by thread: Re: Autorelease Question
  • Next by thread: Re: Autorelease Question
  • Index(es):
    • Date
    • Thread