Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone - UIColor leaking... need to release the object?

Tags:

iphone

I have a bunch of lines like this on my app

UIColor *myColor = [UIColor colorWithRed:corR green:corG blue:corB alpha:1.0];

Instruments are saying these lines are leaking. As this is not formally, as far as I see, an alloc operation (isn't it?) I don't saw the need to release the object, but as instruments are complaining, I added several lines as

[myColor release]

after using the variable, to please the beast.

Will I have problems doing this, like crashes or something?

Apparently doing this is solving the problem, but I am not comfortable to release an object that was not allocated.

What do you think?

thanks.


E D I T

I suppose this is a xcode problem or a framework leak. To prove that I replace the lines with

UIColor *myColor = [[UIColor alloc] initWithRed:corR green:corG blue:corB alpha:1.0];

and then the object could be safely released...

doing that, solved the problem.

like image 216
Duck Avatar asked Oct 06 '10 00:10

Duck


2 Answers

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).

like image 197
Jason Coco Avatar answered Sep 29 '22 01:09

Jason Coco


If that's all you are doing, myColor 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. Evvarrrrrrrrr. But I suspect that you just are retaining it somewhere, and don't even notice it.

like image 38
Jonathan Sterling Avatar answered Sep 29 '22 01:09

Jonathan Sterling