【发布时间】:2011-02-24 01:32:15
【问题描述】:
我对 iPhone 上的 Objective C 很陌生,所以我希望你不会因为问一个简单的问题而杀了我。
我制作了一个运行良好的应用程序,除了 Instruments 报告来自下面的类的内存泄漏。我用它来存储一个类的设置,然后从另一个类中检索它们。这些设置存储在一个文件中,因此每次运行应用程序时都可以检索它们。
我能做些什么来释放“设置”,有什么可以做的以更智能的方式调用(使用)类?
谢谢
----- Below is Settings.m -----
#import "Settings.h"
@implementation Settings
@synthesize settings;
-(NSString *)dataFilePath // Return path for settingfile, including filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kUserSettingsFileName];
}
-(NSMutableArray *)getParameters // Return settings from disk after checking if file exist (if not create with default values)
{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) // Getting data from file
{
settings = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
}
else // Creating default settings
{
settings = [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithInteger:50],
[NSNumber numberWithInteger:50],
nil];
[settings writeToFile:[self dataFilePath] atomically:YES];
}
return settings;
}
----- Below is my other class from where I call my Settings class -----
// Get settings from file
Settings *aSetting = [[Settings alloc] init];
mySettings = [aSetting getParameters];
[aSetting release];
【问题讨论】:
标签: objective-c class memory-leaks