Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden

References: 
 >Re: String memory leak (From: Andy Lee <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.