How to solve Memory leaks in Lib Xml Parser in objective-C where the list is returned?

I got leaks in Lib Xml Parser while retrieving the data from the net,

Here in the below code, I have allocated the list

- (void)getCustomersList
{
// make an operation so we can push it into the queue
SEL method = @selector(parseForData);
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self  selector:method object:nil];
customersTempList = [[NSMutableArray alloc] initWithCapacity:20];// allocated list

[self.retrieverQueue addOperation:op];
[op release];
}

// return each recode 
// in parser .m class one of the condition in endElement where it shows a leak.

else if(0 == strncmp((const char *)localname, kCustomerElement, kCustomerElementLength)) 
{
    [customersTempList addObject:customer];
    printf("\n no of objects in temp list:%d", [customersTempList count]);
    if ([customersTempList count] == 20)
    {
        NSMutableArray* argsList = [customersTempList copy];//////////////////////here it is showing leak.
        printf("\n Calling reload data with %d new objects", [argsList count]);
        SEL selector = @selector(parser:addCustomerObject:);
        NSMethodSignature *sig = [(id)self.delegate methodSignatureForSelector:selector];
        if(nil != sig && [self.delegate respondsToSelector:selector]) {
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
            [invocation retainArguments];
            [invocation setTarget:self.delegate];
            [invocation setSelector:selector];
            [invocation setArgument:&self atIndex:2];
            [invocation setArgument:&argsList atIndex:3];
            [invocation performSelectorOnMainThread:@selector(invoke) withObject:NULL waitUntilDone:NO];

        }
        [customersTempList removeAllObjects];
    }
}

// returned the list after all the records are stored in the list
else if(0 == strncmp((const char *)localname, kCustomersElement, kCustomersElementLength)) {
                printf("\n Calling reload data with %d new objects", [customersTempList count]);
            NSMutableArray* argsList = [customersTempList copy];
    printf("\n Calling reload data with %d new objects", [argsList count]);

    SEL selector = @selector(parser:addCustomerObject:);
    NSMethodSignature *sig = [(id)self.delegate methodSignatureForSelector:selector];
    if(nil != sig && [self.delegate respondsToSelector:selector]) {
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
        [invocation retainArguments];
        [invocation setTarget:self.delegate];
        [invocation setSelector:selector];
        [invocation setArgument:&self atIndex:2];
        [invocation setArgument:&argsList atIndex:3];
        [invocation performSelectorOnMainThread:@selector(invoke) withObject:NULL waitUntilDone:NO];
    }
    [customersTempList removeAllObjects];

}
}

please help me out of this, Thanks, Madan.

Answers


Well, you're leaking argsList because you create it (via copy) and never release it.

Since the only thing you do with it is set it as an argument of invocation, and since you've told invocation to retain its arguments, you can easily either put [argsList release] at the end of the block, or set it to autorelease in the first place.

(It looks like there's a risk of leaking customersTempList as well, since getCustomersList creates it without checking to see whether it already exists -- if you call that several times, it'll leak.)


Need Your Help

iphone device orientation

iphone objective-c cocoa-touch uikit orientation

During inAppPurchase, the storeKit will ask the username and password

migrating to dbcontext LINQ where clause string parameter

c# linq where-clause dbcontext

I'm migrating my project from ObjectContext to DbContext and I have a problem with this sentence: