1

从网上检索数据时,我在 Lib Xml Parser 中发生了泄漏,

在下面的代码中,我已经分配了列表

- (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];

}
}

请帮我解决这个问题,谢谢,马丹。

4

1 回答 1

1

好吧,您正在泄漏argsList,因为您创建它(通过copy)而不是release它。

由于您对它所做的唯一事情是将其设置为 的参数invocation,并且由于您已告知invocation要保留其参数,因此您可以轻松地将其放在[argsList release]块的末尾,或者autorelease首先将其设置为。

(看起来也有泄漏的风险customersTempList,因为getCustomersList创建它时没有检查它是否已经存在——如果你多次调用它,它会泄漏。)

于 2010-06-14T09:22:29.120 回答