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

Re: String memory leak


  • Subject: Re: String memory leak
  • From: Bruce Truax <email@hidden>
  • Date: Sat, 01 Apr 2006 17:11:38 -0500
  • Thread-topic: String memory leak



On 4/1/06 10:07 AM, "Andy Lee" <email@hidden> wrote:

> On Apr 1, 2006, at 7:30 AM, Bruce Truax wrote:
>> The command line tool "leaks" is telling me that I have an 80 byte
>> leak of
>> type NSCFString everytime I execute the following method:
>>
>> - (void)GLASS:(NSString *)parameterString
>> {
>>     [aSurface setGlass:[NSString stringWithFormat:
>>         @"GLASS %@", parameterString]];
>>     [aSurface setSurfaceNumber:surfaceNumber];
>>     surfaceNumber++;
>>     [surfaceArray    addObject:aSurface];
>>     aSurface = [[ACSurface alloc]init];
>>
>> }
>
> Every time you call this method you're setting aSurface to a new
> instance of ACSurface without releasing the previous value of
> aSurface.  So you are leaking instances of ACSurface, each of which
> contains an NSCFString.

I tried releasing aSurface after the addObject command but then my program
crashed because I guess that addObject does not retain the object which is
added.  Hence, there is no release.


>
> You could create a -setSurface: method, using one of the proper
> patterns for a setter method, and call that instead of using
> assignment to set aSurface.  Or, if this is the only place you set
> aSurface, you could use that pattern right in this method.

I may give this a try.

>
>> Should I be using the following instead?
>>
>>   [aSurface setGlass:[[NSString alloc] initWithFormat:
>>         @"GLASS %@", parameterString]];
>
> No, this would make the problem worse, because the string you would
> be passing to -setGlass: would be over-retained.
>

That is what I thought.

Bruce


 _______________________________________________
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: 
 >Re: String memory leak (From: Andy Lee <email@hidden>)

  • Prev by Date: Re: String memory leak
  • Next by Date: Zombie: XCode detects a phantom file that I can't find.
  • Previous by thread: Re: String memory leak
  • Next by thread: Re: String memory leak
  • Index(es):
    • Date
    • Thread