iphone app NSNumber memory leak

2019-07-02 07:05发布

问题:

I'm having a memory leak and I have no clue where it comes from and how to fix it.

At some point i calculate the distance between 2 locations.

double calc = [self getDistance:location to:otherLocation];
NSNumber *distance = [NSNumber numberWithDouble:calc];

in instruments i get as leaked object NSCFNumber and it identifies

NSNumber *distance = [NSNumber numberWithDouble:calc];

as the line causing this.

I'm at a loss. Please help.

thanks in advance, Michiel

回答1:

There's nothing wrong with what you're doing there. The NSNumber has a +0 retain count (autoreleased), so you're probably retaining that NSNumber somewhere else and forgetting to release it.



回答2:

Leaks shows you where an object that is leaking is CREATED.

If you think about it, there's really nothing else Leaks can show you - it's leaking because the object should have been released at some point, and Leaks can't show you code that does not exist!



回答3:

I don't believe that you have problem on the NSNumber line.

I have run the following code and I've got no problems

code>double calc = 10.1234567;
    NSNumber *distance = [NSNumber numberWithDouble:calc];

    NSLog(@"calc: %f | %f", calc, [distance doubleValue]);

I believe that your problem is in your method "getDistance: to:"

Do like I have done. put a static value, instead of the method and check if it is ok.

Cheers,
VFN