• 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: Crash after weeks of doing nothing to my code
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Crash after weeks of doing nothing to my code


  • Subject: Re: Crash after weeks of doing nothing to my code
  • From: Greg Hurrell <email@hidden>
  • Date: Wed, 3 Aug 2005 10:48:07 +0200

El 03/08/2005, a las 4:24, Nick Zitzmann escribió:

oneStringFromArray = [arrayToStripNewLineCharFromString objectAtIndex:i];

Here the program leaks memory, because you blew away the pointer to the string you created one line before.


oneStringFromArray = [cookieString stringByAppendingString:oneStringFromArray];

Here you overwrite the pointer again.

Just to clarify, overwriting pointers isn't *always* a bad thing. If you have a pointer to an autoreleased object and you overwrite it the object will still get released and won't leak. It's only a problem when you have a non-autoreleased object: when you overwrite the pointer you no longer have a way to release the object so it leaks. Example:


// no leak
NSString *object = [dictionary objectForKey:@"Key"];
object = [dictionary objectForKey:@"Other key"];

// leaks, no way to release allocted string
NSMutableString *string = [[NSMutableString alloc] init];
string = [dictionary objectForKey:@"Another key"];

Having said that, before reusing a pointer to an autoreleased object you should probably weigh up the benefits and costs in terms of code readability/clarity and compactness. If recycling the variable could lead to confusion or mistakes later on you should avoid doing it. On the other hand, if doing it is going to make a lengthy method much clearer and easier to understand (ie. by replacing a bunch of references to "string1", "string2", "string3" etc with a single reference to "string") then it might be worth doing (provided you understand when this will and won't cause you to leak).

Greg

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Crash after weeks of doing nothing to my code (From: Adam <email@hidden>)
 >Re: Crash after weeks of doing nothing to my code (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Question on guidelines of integrating C++ calls inside an Obj C class
  • Next by Date: Re: Fullscreen window after hiding Dock works in 10.4 but not 10.3
  • Previous by thread: Re: Crash after weeks of doing nothing to my code
  • Next by thread: splitview location for documents...
  • Index(es):
    • Date
    • Thread