InAppSettings icon indicating copy to clipboard operation
InAppSettings copied to clipboard

A few leaks found

Open kgn opened this issue 14 years ago • 6 comments

First, a big thanks for this code!! After running my app through Instruments, I found a few leaks. Below is the info I extracted from Instruments. Let me know if you need anything else. LEAKED OBJECT: Malloc 16 bytes EVENT TYPE: Malloc RESPONSIBLE CALLER: [InAppSettingsReaderRegisterDefaults loadFile]

  • (void)loadFile:(NSString *)file{ if the file is not in the files list we havn't read it yet NSInteger fileIndex = [self.files indexOfObject:file]; if(fileIndex == NSNotFound){ [self.files addObject:file]; load plist NSDictionary *settingsDictionary = [[NSDictionary alloc] initWithContentsOfFile:InAppSettingsFullPlistPath(file)]; NSArray *preferenceSpecifiers = [settingsDictionary objectForKey:InAppSettingsPreferenceSpecifiers]; NSString *stringsTable = [settingsDictionary objectForKey:InAppSettingsStringsTable]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; for(NSDictionary *eachSetting in preferenceSpecifiers){ InAppSettingsSpecifier *setting = [[InAppSettingsSpecifier alloc] initWithDictionary:eachSetting andStringsTable:stringsTable]; if([setting isValid]){ if([setting isType:InAppSettingsPSChildPaneSpecifier]){ [self loadFile:[setting valueForKey:InAppSettingsSpecifierFile]]; }else if([setting hasKey]){ if([setting valueForKey:InAppSettingsSpecifierDefaultValue]){ [self.values setObject:[setting valueForKey:InAppSettingsSpecifierDefaultValue] forKey:[setting getKey]]; } } } [setting release]; } [pool drain]; [settingsDictionary release]; } } LEAKED OBJECT: NSCFDictionary EVENT TYPE: Malloc RESPONSIBLE CALLER: [InAppSettingsReaderRegisterDefaults init]
  • (id)init{ self = [super init]; if (self != nil) { self.files = [[NSMutableArray alloc] init]; self.values = [[NSMutableDictionary alloc] init]; [self loadFile:InAppSettingsRootFile]; [[NSUserDefaults standardUserDefaults] registerDefaults:self.values]; } return self; } LEAKED OBJECT: NSCFDictionary EVENT TYPE: CFRetain RESPONSIBLE CALLER: [InAppSettingsReaderRegisterDefaults setValues] NO CODE HIGHLIGHTED FOR THIS ONE AND I DIDN'T EVEN SEE THIS METHOD ** LEAKED OBJECT: NSCFDictionary EVENT TYPE: CFRelease RESPONSIBLE CALLER: [InAppSettingsReaderRegisterDefaults dealloc] NO CODE HIGHLIGHTED FOR THIS ONE ** LEAKED OBJECT: NSArrayM EVENT TYPE: Malloc RESPONSIBLE CALLER: [InAppSettingsReaderRegisterDefaults init]
  • (id)init{ self = [super init]; if (self != nil) { self.files = [[NSMutableArray alloc] init]; self.values = [[NSMutableDictionary alloc] init]; [self loadFile:InAppSettingsRootFile]; [[NSUserDefaults standardUserDefaults] registerDefaults:self.values]; } return self; } LEAKED OBJECT: NSArrayM EVENT TYPE: Retain RESPONSIBLE CALLER: [InAppSettingsReaderRegisterDefaults setValues] NO CODE HIGHLIGHTED FOR THIS ONE AND I DIDN'T EVEN SEE THIS METHOD ** LEAKED OBJECT: NSArrayM EVENT TYPE: Release RESPONSIBLE CALLER: [InAppSettingsReaderRegisterDefaults dealloc] NO CODE HIGHLIGHTED FOR THIS ONE **

kgn avatar Jun 02 '11 08:06 kgn

Glad I read this issue before adding this to my project. Personally I would prefer just to remove the "self." and do a direct assignment because all of these seem to be in the init functions anyway (pointers should be nil at this point). When using "self." the setter is used, which retains the object, and you're passing an autoreleased object. If you assign it directly you avoid using an autorelease and the retainCount is balanced in the dealloc regardless of whether the property is "retain" or "assign". If for some reason later the property was changed to "assign" you won't loose the object by doing a direct assignment (because object ins alloc'ed in init, and released in dealloc).

kgn avatar Jun 02 '11 08:06 kgn

I found some leaks and fixed them. I attached the patch.

patch: https://bitbucket-assetroot.s3.amazonaws.com/keegan3d/inappsettings/20101015/27/InAppSettings.20101014.diff

kgn avatar Jun 02 '11 08:06 kgn

From and email: I’ve found that Instruments/leak detection is finding a number of leaks, even when using sample app. I’m hoping it is something I am doing incorrectly here. For example, when I init default values from my app delegate, leaks are reported in the in the InAppSetttingsRegisterDefaults loadFile function. There are 12 leaks reported. It appears that the retain count of ‘setting’ is 2 immediately before its release. I’d appreciate any suggestions you may have.

kgn avatar Jun 02 '11 08:06 kgn

I had posted this original message back on June 2 and was wondering if you would have an updated version of the code soon with the memory leaks fixed?

Thanks

tblack2200 avatar Jul 26 '11 12:07 tblack2200

I haven't been writing iOS apps recently so I haven't looked into this issue.

kgn avatar Jul 26 '11 17:07 kgn

I'll need to look into this, hopefully it's fixed with ARC

kgn avatar Jan 24 '13 20:01 kgn