NSArray与componentsSeparatedByString内存泄漏

时间:2011-04-05 12:45:39

标签: objective-c memory-leaks instruments

我在使用此功能时出现内存泄漏问题。我认为使用componentsSeparatedByString创建一个NSArray是自动释放,但是仪器似乎表明NSArray aPair有泄漏。为什么它会在那里表明泄漏,而不是以同样的方式创建其他NSArrays?

-(void) checkRequest: (NSString *)request view:(UIViewController *)theView webView:(UIWebView *)wView
{
    //NSLog(@"JSResponder - checkRequest()");

    NSString *aRequest = [NSString stringWithString:request];

    NSArray *urlArray = [aRequest componentsSeparatedByString:@"?"];

    if([urlArray count] > 1)
    {
        NSString *paramsString = [urlArray lastObject];
        NSString *cmd = @"";

        NSArray *urlParamsArray = [paramsString componentsSeparatedByString:@"&"];  
        int numCommands = [urlParamsArray count];

        NSMutableDictionary *paramsWithNames = [[NSMutableDictionary alloc ]                        initWithCapacity:numCommands];

        for (NSString *elementPair in urlParamsArray)
        {
            NSArray *aPair = [elementPair componentsSeparatedByString:@"="];

            NSString *aKey = [aPair objectAtIndex:0];
            NSString *aParam = [aPair objectAtIndex:1]; 

            if([aKey compare:@"_command"] == NSOrderedSame)
            {
                cmd = aParam;
            }   
            else 
            {
                [paramsWithNames setValue: aParam forKey:aKey];
            }
        }

        [self executeCommand: cmd withParams: paramsWithNames view:theView webView:wView];

        [paramsWithNames release];
    }
}

此功能由以下内容调用:

    - (void)pullJSEvent:(NSTimer*)theTimer
{
    NSLog(@"MainView - pullJSEvent()");

    NSString *jsCall = [NSString stringWithString:@"if(typeof checkOBJCEvents == 'function'){checkOBJCEvents();}"];

    NSString *jsAnswer = [[webView stringByEvaluatingJavaScriptFromString:jsCall] retain];

    if([jsAnswer compare:@"none"] != NSOrderedSame)
    {
        //NSLog(@"           answer => %@", jsAnswer);
        [jsResponder checkRequest:jsAnswer view:(UIViewController *)self webView:self.webView];
    }
    [jsAnswer release];
}

感谢您

1 个答案:

答案 0 :(得分:2)

你将不得不更深入地了解Leaks仪器。你正在泄漏数组中的一个字符串,而不是数组本身。泄漏表示该行,因为这是分配数组中的字符串的位置。

进入Leaks,查看泄露的实例,然后单击该小箭头按钮。您将看到泄漏对象的所有保留和释放,这应该指出您的问题。

Leaks