Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...