HiveHicks / HHUnitConverter

Unit conversion library for Objective-C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak in _conversionRulesFromUnit:toUnit:

testower opened this issue · comments

Instruments is reporting a memory leak in _conversionRulesFromUnit:toUnit: in HHUnitConverter.m here:

    NSMutableArray *rules = [NSMutableArray new];

The leak goes away when I change that line to:

    NSMutableArray *rules = [NSMutableArray array];

which creates an autoreleased array. I'm not sure if that's the root cause, but I can't find any other source.

I use the converter through a category:

#import "HHUnitConverter+Temperature.h"

@implementation HHUnitConverter (Temperature)

+ (HHUnitConverter *)sharedTemperatureConverter
{
    static HHUnitConverter *converter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        converter = [HHUnitConverter new];
        [converter letUnit:@"C" convertToUnit:@"F" byMultiplyingBy:(9/5) andAdding:32];
    });
    return converter;
}

- (NSNumber *)fahrenheitFromCelsius:(NSNumber *)celsiusValue
{
    return [self value:celsiusValue.doubleValue convertedFromUnit:@"C" toUnit:@"F"];
}

- (NSNumber *)celsiusFromFahrenheit:(NSNumber *)fahrenheitValue
{
    return [self value:fahrenheitValue.doubleValue convertedFromUnit:@"F" toUnit:@"C"];
}

@end

I'd be happy to send a pull request on the fix, unless you think there is some other cause to the leak.

More information on the leak. There are actually two leaks, one on the NSMutableArray object, but also one Malloc 16 bytes, that stems from this line:

    [rules addObject:[_weights objectForKey:routeStep.edge.name]];

What's the exact code that you use? I'm trying to reproduce the issue with this:

for (int i = 0; i < 100000; i++) {
    [[HHUnitConverter sharedTemperatureConverter] fahrenheitFromCelsius:@(30)];
}

but I can't see any leaks.

Also, do you have ARC on in your project?

I was able to reproduce the reported leaks when I turned off ARC.

HHUnitConverter is meant to be used with ARC only. If you can't use it for your entire project, you could flag HHUnitConverter.m with -fobjc-arc in build settings.

Closing it now. Feel free to reopen if needed.

I am definately using ARC. Sorry I have not been able to respond. I will provide more debugging data tomorrow.

Each call to a converter-method results in the two leaks:

screenshot 2014-04-22 11 06 56
screenshot 2014-04-22 11 07 31
screenshot 2014-04-22 11 07 44