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 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.


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.

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.


--Andy

_______________________________________________
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: 
 >String memory leak (From: Bruce Truax <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.