NSMutableDictionary initWithContentsOfFile 中的内存泄漏
这是我的代码:(customNames和customNamesArray是静态变量)
-(void) loadCustomDataFromDisk
{
NSString *fullPath = [self filePathAndFileName: @"customData.plist"];
if ( ![[NSFileManager defaultManager] fileExistsAtPath: fullPath] )
{
NSLog(@"Loading file fails: File not exist");
customNames = [[NSMutableDictionary alloc] init];
customNamesArray = [[NSMutableArray alloc] init];
}
else
{
NSMutableDictionary *customItems = [[NSMutableDictionary alloc] initWithContentsOfFile: fullPath];
customNames = [customItems objectForKey: @"customNamesDict"];
customNamesArray = [customItems objectForKey: @"customNamesArray"];
if (!customItems)
NSLog(@"Error loading file");
[customItems release];
}
}
-(void) saveCustomDataToDisk
{
NSString *path = [self filePathAndFileName: @"customData.plist"];
NSMutableDictionary *customItems = [[NSMutableDictionary alloc] init];
[customItems setObject: customNames forKey: @"customNamesDict"];
[customItems setObject: customNamesArray forKey: @"customNamesArray"];
BOOL success;
success = [customItems writeToFile:path atomically:YES];
if (!success)
NSLog(@"Error writing file: customDataDict.plist");
[customItems release];
}
根据Build和Analyze,我在加载customItems时存在潜在泄漏
NSMutableDictionary *customItems = [[NSMutableDictionary alloc] initWithContentsOfFile: fullPath];
,根据Instruments,我确实在该部分存在泄漏。但是当我尝试发布或自动发布自定义项目时,我的应用程序崩溃了。即使我将 NSMutableDictionary 更改为 NSDictionary,我仍然存在泄漏。 我该如何解决这个问题?
任何帮助将非常感激。 :) 谢谢 :)
This is my code: (customNames and customNamesArray are static variables)
-(void) loadCustomDataFromDisk
{
NSString *fullPath = [self filePathAndFileName: @"customData.plist"];
if ( ![[NSFileManager defaultManager] fileExistsAtPath: fullPath] )
{
NSLog(@"Loading file fails: File not exist");
customNames = [[NSMutableDictionary alloc] init];
customNamesArray = [[NSMutableArray alloc] init];
}
else
{
NSMutableDictionary *customItems = [[NSMutableDictionary alloc] initWithContentsOfFile: fullPath];
customNames = [customItems objectForKey: @"customNamesDict"];
customNamesArray = [customItems objectForKey: @"customNamesArray"];
if (!customItems)
NSLog(@"Error loading file");
[customItems release];
}
}
-(void) saveCustomDataToDisk
{
NSString *path = [self filePathAndFileName: @"customData.plist"];
NSMutableDictionary *customItems = [[NSMutableDictionary alloc] init];
[customItems setObject: customNames forKey: @"customNamesDict"];
[customItems setObject: customNamesArray forKey: @"customNamesArray"];
BOOL success;
success = [customItems writeToFile:path atomically:YES];
if (!success)
NSLog(@"Error writing file: customDataDict.plist");
[customItems release];
}
According to Build and Analyze, I have a potential leak in loading customItems
NSMutableDictionary *customItems = [[NSMutableDictionary alloc] initWithContentsOfFile: fullPath];
true enough, according to Instruments, I do have a leak in that part. But when I tried release or autoreleasing customItems, my app crashes. Even if I change NSMutableDictionary to NSDictionary, I still have the leak.
How do I fix this?
Any help would be very much appreciated. :) Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须保留customNames 和customNamesArray,因为您正在使用字典customItems 中的引用,并且在传递引用后您将释放它。
customNames = [[customItems objectForKey: @"customNamesDict"] 保留];
customNamesArray = [[customItems objectForKey: @"customNamesArray"] 保留];
现在您可以发布自定义项目。
You have to retain customNames and customNamesArray because you are using reference from dictionary customItems and after passing reference you are releasing it.
customNames = [[customItems objectForKey: @"customNamesDict"] retain];
customNamesArray = [[customItems objectForKey: @"customNamesArray"] retain];
Now you can release customItems.
正如我所见,你的代码是正确的。您可以在这里看到答案,可能会有所帮助 - initWithContentsOfFile 的泄漏问题
我只有一个问题:您创建 NSString *fullPath 并且永远不会释放它。它是自动释放的字符串吗?如果是这样 - 你的代码没问题。
Your code is right as I can see. You can see answer here and may be it helps - Leak problem with initWithContentsOfFile
I have only one question: You create NSString *fullPath and never release it. Is it autoreleased string? If so - your code is fine.