Jonathan
Jonathan

Reputation: 139

CALayer causes memory leaks?

the following code makes my app slower as a snail (it causes a memory leak i think):

CALayer *layer = [[CALayer layer] retain]; 
detailcell.layer.borderColor = [UIColor grayColor].CGColor;
detailcell.layer.borderWidth = 2;
detailcell.layer.shadowColor = [UIColor blackColor].CGColor;
detailcell.layer.shadowOpacity = 1.0;
detailcell.layer.shadowRadius = 5.0;
detailcell.layer.shadowOffset = CGSizeMake(0, 3);
detailcell.layer.cornerRadius = 20;
detailcell.clipsToBounds = NO;
[layer release];

The "detailcell" is a UIView and i create a border and a shadow. If i omit the code, my app runs perfect. After researching the problem via the internet, CALayer *layer = [[CALayer layer] retain]; should be the solution, but the problem is still there. has anyone got an idea to solve the problem?

Thank you very much!

Upvotes: 1

Views: 1776

Answers (2)

Warren Burton
Warren Burton

Reputation: 17378

Where are you calling this code. Somewhere it gets called a lot? e.g like a tableview.

Its not syntax wrong but its not right. And the layer you create does nothing. You are also creating 10 extra obj-c calls with each run through this section.

try this and see if you get any speed up.

CALayer *layer = detailcell.layer; 
layer.borderColor = [UIColor grayColor].CGColor;
layer.borderWidth = 2;
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowOpacity = 1.0;
layer.shadowRadius = 5.0;
layer.shadowOffset = CGSizeMake(0, 3);
layer.cornerRadius = 20;
detailcell.clipsToBounds = NO;

Without knowing where you are using this code thats the most you can do.

Upvotes: 1

Michael Bai
Michael Bai

Reputation: 586

I have similar problem, I have used CALayer to create a button with custom cornerRadius and so on, then add it to a UITableViewCell, and when I scroll the tableView, the memory showed in Instruments got higher and higher, and the app got more and more slower. About solving this solution, I don't have a very good idea, but I think you can make a suitable image instead. This is the simplest way I think.

Upvotes: 0

Related Questions