开发者

iPhone memory leak when method returns object

I'm developping on iOS 4 and I've got this method in my appDelegate. It is called from a couple of tableviews datasource delegates. Instruments gave me these (on device and simulator) Malloc 512 bytes, Malloc 512 bytes, NSConcreteMapTable (Foundat开发者_运维百科ion). The Mallocs don't show any responsible library.

Here's the method returning the object:

- (NSXMLParser *) getXmlParserFrom:(NSString *)remoteFile andCacheToFile:(NSString *) fileName forceRefresh:(BOOL) doRefresh {  
    NSXMLParser *xmlParser;

    //FIRST TRY TO LOAD THE XML FROM CACHED FILE  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

    NSString *xmlDocumentFromCache = [[NSString alloc] initWithContentsOfFile:filePath];

    if ( xmlDocumentFromCache && !doRefresh ) {  
        NSData *xmlData = [NSData dataWithContentsOfFile:filePath];
        xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
    }  else {  
        NSURL *xmlFileURL = [NSURL URLWithString:remoteFile];
        NSString *contentsOfRemoteFile = [NSString stringWithContentsOfURL:xmlFileURL];

        //CACHE THE FILE  
        BOOL cacheResult = [contentsOfRemoteFile writeToFile:filePath atomically:YES];
        xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlFileURL];
    }  
    [xmlDocumentFromCache release];

    return [xmlParser autorelease];
}


I just fixed this by using the method outlined in this post.

It's a workaround, but it works.

On another note, I have found that Instruments works reliably in Lion/Xcode 4.1 if you always run it on the device, as opposed to the simulator. On the simulator, it seems to have a devil of a time attaching to the process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜