Memory Leak in returning NSMutableArray from class
Posted
by Structurer
on Stack Overflow
See other posts from Stack Overflow
or by Structurer
Published on 2010-05-26T08:09:12Z
Indexed on
2010/05/26
8:11 UTC
Read the original article
Hit count: 222
Hi
I am quite new to Objective C for the iPhone, so I hope you wont kill me for asking a simple question.
I have made an App that works fine, except that Instruments reports memory leaks from the class below. I use it to store settings from one class and then retrieve them from another class. These settings are stored on a file so they can be retrieved every time the App is ran.
What can I do do release the "setting" and is there anything that can be done to call (use) the class in a smarter way?
Thanks
----- 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];
© Stack Overflow or respective owner