CALayer memory leak

Hope someone can help me here because I am losing the desire to live with this memory problem. When I click on a specific view controller, the tools show a growth of about 3 MB, most of which come from CALayer objects that belong to every control that I have on the view controller (mostly text fields and labels). When I exit this view controller, these CALayer distributions remain in memory, in fact, every time I click and exit the controller, each time I get 3 MB growth.

So my question is how to remove them from memory, is there something I need to do when releasing the controller? The only link in my code to the control layer is to create rounded corners and border widths (layer.cornerRadius = 10), but I still get growth even when I comment on it. The controller contains a table view in which each cell contains 3 text fields, 2 buttons and an image that are created programmatically.

-(UIButton*) formatButton:(UIButton*)button
{
button.layer.cornerRadius = 10;
button.clipsToBounds = YES;
button.layer.borderColor=[[UIColor lightGrayColor] CGColor];
button.layer.borderWidth=1.0;
[button.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:20]];
[button setTitle:@"0" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

return button;
}

-(UITextField*)formatTextField:(UITextField*)textField
{
[textField setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:20]];
textField.backgroundColor=[UIColor whiteColor];
textField.layer.borderColor=[[UIColor lightGrayColor] CGColor];
textField.layer.cornerRadius = 10;
textField.layer.borderWidth=1.0;
[textField setTextAlignment:NSTextAlignmentCenter];
textField.clipsToBounds=YES;
textField.text=@"";
textField.enabled=NO;

return textField;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCellID" forIndexPath:indexPath];
// Configure the cell...
OrderedProduct *orderedProduct=self.orderedProductsArray[indexPath.row];
//NSLog (@"ordered Product %@",orderedProduct.productID);

UITextField *qualityField=[[UITextField alloc] initWithFrame:CGRectMake(113,23,324,30)];
qualityField=[self formatTextField:qualityField];
[cell addSubview:qualityField];

UITextField *sizeField=[[UITextField alloc] initWithFrame:CGRectMake(445, 24, 142, 30)];
sizeField=[self formatTextField:sizeField];
[cell addSubview:sizeField];

UITextField *totalPriceField=[[UITextField alloc] initWithFrame:CGRectMake(786,24,137,30)];
totalPriceField=[self formatTextField:totalPriceField];
[cell addSubview:totalPriceField];

UIButton *quantityButton=[[UIButton alloc] initWithFrame:CGRectMake(595,24,60,30)];
quantityButton=[self formatButton:quantityButton];
[cell addSubview:quantityButton];

UIButton *priceButton=[[UIButton alloc] initWithFrame:CGRectMake(663,24,119,30)];
priceButton=[self formatButton:priceButton];
[cell addSubview:priceButton];

UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(5,15,100,50)];
[cell addSubview:imageView];

UIButton *removeButton=(UIButton*)[cell viewWithTag:kRemoveButton];

if (self.selectedOrder.status==[NSNumber numberWithInt:kOrderStatusProvisional])
{
    quantityButton.enabled=YES;
    quantityButton.backgroundColor=[Settings getInstance].enabledButtonColor;
    [quantityButton addTarget:self action:@selector(quantityButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    priceButton.enabled=YES;
    priceButton.backgroundColor=[Settings getInstance].enabledButtonColor;
    removeButton.hidden=NO;
    [removeButton addTarget:self action:@selector(removeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
    quantityButton.enabled=NO;
    quantityButton.backgroundColor=[Settings getInstance].disabledButtonColor;
    priceButton.enabled=NO;
    priceButton.backgroundColor=[Settings getInstance].disabledButtonColor;
    removeButton.hidden=YES;
}

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setCurrencyCode:@"GBP"];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
imageView.image=[self.imageCache objectForKey:orderedProduct.product.productImageName];

UITapGestureRecognizer *tgr=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTapped:)];
imageView.userInteractionEnabled=YES;
[imageView addGestureRecognizer:tgr];
qualityField.text=orderedProduct.product.name;
sizeField.text=orderedProduct.product.productSize.productSize;
quantityButton.titleLabel.text=[NSString stringWithFormat:@"%@",orderedProduct.quantity];
[quantityButton setTitle:[NSString stringWithFormat:@"%@",orderedProduct.quantity] forState:UIControlStateNormal];

float price=[orderedProduct.price integerValue]/100; // server price is in pennies
float totalPrice=price*[orderedProduct.quantity integerValue];
[priceButton setTitle:[numberFormatter stringFromNumber:[NSNumber numberWithFloat:price]] forState:UIControlStateNormal];

totalPriceField.text=[numberFormatter stringFromNumber:[NSNumber numberWithFloat:totalPrice]];

return cell;
}

Impossible to send images, but below is a rough description of what “Tools” says,

Snapshot growth

Generation B 3.64MB

VM: UILabel (CALayer) 964KB

VM: UITextFieldLabel (CALayer) 792KB

VM: CoreAnimation 788KB

+4
source share

Source: https://habr.com/ru/post/1546115/


All Articles