• 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: Another memory question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Another memory question


  • Subject: Re: Another memory question
  • From: Glen Simmons <email@hidden>
  • Date: Tue, 26 Aug 2003 14:54:25 -0500

On Tuesday, August 26, 2003, at 01:53 PM, James Ludtke wrote:

In the code fragment below I reassign a new value to an
NSString. (I have seen others do that as well.)

- (void)awakeFromNib {

NSString *demoFile;

demoFile = [NSString stringWithString: @"~/Application User
Data/dsf.plist"];
demoFile = [demoFile stringByExpandingTildeInPath];
// more code
}

If I have understood it correctly, the second assignment creates
a new memory block for the string, and the first string is still
in memory, but can no longer be accessed.
It can no longer be accessed by *you*, but it has been added to the autorelease pool before being returned to you.

My questions are:

1) Will the auto release process release the first block or
will there be a memory leak?
No memory leak - autorelease is your friend.

2) Is the above example good practice, or would the following
have been better:
Umm, not quite.


- (void)awakeFromNib {

NSString *demoFile;

NSString *temp = [[NSString alloc] init];
You've alloced a new object, so you're responsible for releasing it.
temp = [NSString stringWithString: @"~/Application User Data/dsf.plist"];
Here, you assign the address of another object to the pointer variable temp, thus losing your reference to the original object. This causes a leak. This new object is autoreleased before it's returned to you, so you shouldn't release it.
demoFile = [temp stringByExpandingTildeInPath];
A third object is created, autoreleased and returned and you've assigned it to demoFile.
[demoFile release];
You now release demoFile which you haven't alloced, copied or retained. When the autorelease pool is released, this app will crash. This is a somewhat difficult bug to track down as the crash does not happen close to the actual bug.
// more code
}

I'd suggest reading some more about Cocoa memory management. Most people seem to overthink it when they are starting. It's (usually) quite easy. Try http://www.stepwise.com/StartingPoint/Cocoa.html, specifically the "Cocoa Basics" section.

HTH,
Glen
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Another memory question (From: James Ludtke <email@hidden>)

  • Prev by Date: Re: Another memory question
  • Next by Date: Re: Formats supported by NSImage?
  • Previous by thread: Re: Another memory question
  • Next by thread: RE: Another memory question
  • Index(es):
    • Date
    • Thread