方法返回对象时 iPhone 内存泄漏

发布于 2024-10-02 04:20:21 字数 1375 浏览 2 评论 0原文

我正在 iOS 4 上进行开发,我的 appDelegate 中有这个方法。它是从几个表视图数据源委托调用的。仪器给了我这些(在设备和模拟器上) Malloc 512 字节、Malloc 512 字节、NSConcreteMapTable(基础)。 Malloc 没有显示任何负责任的库。

这是返回对象的方法:

- (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'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 (Foundation). 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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2024-10-09 04:20:21

我刚刚使用

这是一个解决方法,但它确实有效。

另一方面,我发现如果您总是在设备上而不是模拟器上运行 Instruments,它可以在 Lion/Xcode 4.1 中可靠地工作。在模拟器上,这个过程似乎花费了很长的时间。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文