ios - Objective-C,NSMutableData 未发布

标签 ios objective-c macos cocoa nsmutabledata

我的应用有时会读取数据、使用数据,然后可能需要稍后再次读取。我注意到,如果我在数据发布后读取它,但如果我碰巧再次读取它,它就永远不会被发布。

为什么再次读取数据不释放?

代码:

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self readData]; // read one
    NSLog(@"read1");
    sleep(1);
    [self readData2];
}

- (void)readData2 {
    [self readData]; // read two
    NSLog(@"read2");
}

- (void)readData {
    __block NSMutableData *data = [NSMutableData dataWithContentsOfFile:@"test"]; // file is 125 MB
    NSUInteger size = [data length];
        for (NSUInteger i = 0; i < size; i++) {
            // do stuff
        }
        return;
}
@end

enter image description here

最佳答案

尝试将 dataWithContentsOfFile:options:error 与选项一起使用:

NSDataReadingMappedIfSafe

A hint indicating the file should be mapped into virtual memory, if possible and safe.

虽然 dataWithContentsOfFile 的描述没有说明,但不使用 options 可能会导致数据被保留,否则。

dataWithContentsOfFile

This method is equivalent to dataWithContentsOfFile:options:error: with no options. If you need to know what was the reason for failure, use dataWithContentsOfFile:options:error:.

关于ios - Objective-C,NSMutableData 未发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553067/

相关文章:

ios - 在 UITableView 中验证 n 个 UITextfields

ios - 调试时 Xcode 中的无效 ObjC 对象错误

macos - Mac Catalyst 构建失败并显示 "building for Mac Catalyst-x86_64 but attempting to link with file built for Mac Catalyst-arm64"

ios - 使用 Health Kit 的通用应用程序

iphone - 找到多个名为 'tag' 的方法

ios - 编辑点击的 CollectionViewCell

objective-c - NSWindowController 显示窗口 :nil does nothing

ios - xCode 6.2 项目不会使用 xCode 6.3 和 Swift 1.2 存档(swiftc 失败,退出代码为 1)

c++ - 代码块 osx 快板

c++ - 使用 Netbeans (linux/mac) 为 dylib 设置当前/兼容版本