I am new to iPhone objectiveC development. I am getting memory leaks when I run the following method.
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"dbname.sqlite"];
}
and I have found that if I trim it down to just the following… it still leaks
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
return nil;
}
so i tried releasing the paths variable with the following, which still leaks memory.
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
[paths release];
return nil;
}
To detect the leak, I am running it in the profiler with the following loop:
for (int iLoop = 0; iLoop < 30; iLoop++) {
NSString *dbPath = [self getDBPath];
[dbPath release];
sleep(1);
}
The amount of memory associated with NSPathStore2 and NSArrayM continues to grow.
Any suggestions on what I a might be doing wrong?
thanks!
Your code for getDBPath is fine, but it is probably called from a context without any autorelease pool.
Fix it by allocating your own pool in the loop code: