Big time Leaking in Objective-C Category

Posted by Daniel Amitay on Stack Overflow See other posts from Stack Overflow or by Daniel Amitay
Published on 2010-11-15T06:41:47Z Indexed on 2011/01/09 21:53 UTC
Read the original article Hit count: 164

I created a custom NSString Category which lets me find all strings between two other strings. I'm now running into the problem of finding that there are a lot of kBs leaking from my script. Please see code below:

    #import "MyStringBetween.h"

@implementation NSString (MyStringBetween)

-(NSArray *)mystringBetween:(NSString *)aString and:(NSString *)bString;
{
    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];

    NSArray *firstlist = [self componentsSeparatedByString:bString];
    NSMutableArray *finalArray = [[NSMutableArray alloc] init];


    for (int y = 0; y < firstlist.count - 1 ; y++) {
        NSString *firstObject = [firstlist objectAtIndex:y];
        NSMutableArray *secondlist = [firstObject componentsSeparatedByString:aString];
        if(secondlist.count > 1){

            [finalArray addObject:[secondlist objectAtIndex:secondlist.count - 1]];
        }
    }

    [autoreleasepool release];

    return finalArray;
}
@end

I admit that I'm not super good at releasing objects, but I had believed that the NSAutoreleasePool handled things for me.

The line that is leaking:

NSMutableArray *secondlist = [firstObject componentsSeparatedByString:aString];

Manually releasing secondlist raises an exception.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c