Question

I have this situation:

UIColor *color = [[UIColor alloc] initWithRed:0/255.f green:156/255.f blue:255/255.f alpha:1];
self.lightBlue = color;
color = nil;
[color release];

And receive memoery leak in UIColor *color = [[UIColor alloc] initWithRed:0/255.f green:156/255.f blue:255/255.f alpha:1];

The situation from Instruments: instruments situation

Any ideas?

Was it helpful?

Solution

You are setting your local variable 'color' to nil and then doing [nil release] which does nothing.

If you really need 'color = nil', you should do it after the release.

OTHER TIPS

The leaks tool shows you where something that is leaking, was allocated.

So what it's saying is, after you assigned these values they were never released - which seems like it means your navigation controller is not being released when it should be. Don't release the object, you don't own it and you will eventually get crashes. UIColor is probably just caching these colors for you, and Instruments has no way of knowing this so it reports them as leaks (basically stuff that got created and you don't have a reference to anymore but hasn't been deallocated).

Try running instruments for some time (using the simulator) and then sending a memory warning to see if UIColor will purge its cache. Either way, there isn't anything you can really do to fix leaks happening inside core frameworks, so don't try. Just make sure you're not actually leaking them somehow (like retaining them at some point and never releasing them).

Bottom line is if the above code is all that you are doing, color is most definitely not leaking. If you are retaining that object anywhere else without releasing it, it is leaking.

Never release an object that you don't remember retaining. EVER. But I suspect that you just are retaining it somewhere, and don't even notice it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top