Lo he probado y parece que sí. Entonces mi pregunta es SIEMPRE incrementa el conteo retenido.does addSubview increment retain count?
Así que cada vez que hacer algo como esto:
UIView *theView = [[[UIView alloc] initWithFrame:(CGRect)aFrame] autorelease];
[self.view addSubview:theView];
¿Estoy realmente perdiendo memoria?
que tienen una propiedad global @property (nonatomic, retain) UILabel *ingredientsTextLabel;
la que crear una instancia en viewDidLoad
con este código:
acabo la propiedad llamada, theres ninguna propiedad para que en mi cabecera, por lo que no get y set. En mi viewDidLoad
:
ingredientsTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ingredientsScrollView.frame.size.width, ingredientsScrollView.frame.size.height)];
[ingredientsTextLabel setBackgroundColor:[UIColor clearColor]];
[ingredientsTextLabel setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[ingredientsTextLabel setText:ingredientsText];
[ingredientsTextLabel setNumberOfLines:0];
[ingredientsTextLabel setLineBreakMode:UILineBreakModeWordWrap];
NSLog(@"%i",[ingredientsTextLabel retainCount]); // here retain count is 1
CGSize maxSize = CGSizeMake(ingredientsScrollView.frame.size.width, 9999);
CGSize ingLabSize = [ingredientsText sizeWithFont:ingredientsTextLabel.font
constrainedToSize:maxSize
lineBreakMode:ingredientsTextLabel.lineBreakMode];
[ingredientsTextLabel setFrame:CGRectMake(ingredientsTextLabel.frame.origin.x, ingredientsTextLabel.frame.origin.x, ingredientsTextLabel.frame.size.width, ingLabSize.height)];
[ingredientsScrollView addSubview:ingredientsTextLabel];
NSLog(@"%i",[ingredientsTextLabel retainCount]); // here retain count is 2!
Ahora pensé que esto iba a funcionar y luego en dealloc que puede liberar ingredientsTextLabel
, pero el recuento de retener es 2, por lo que necesito para liberar i también después de que addSubview así? ¡No me di cuenta de que esto sucede! :(
Correcto. Thomas, no necesitas hacer otra versión después de addSubview: solo debes preocuparte por liberar ONCE por alloc/init. Si addSubview: aumenta el conteo de retención, es su responsabilidad liberarlo. – pablasso
oh cierto ... ya veo. Gracias. ¿Significa eso que si aloco/inicio un UIView, lo agrego como una subvista de 'self.view' y luego lo libero el conteo de retención sigue siendo 1? Por lo tanto, aunque lo publiqué, ¿puedo enviar mensajes? Gracias –
Sí, eso es correcto. Pero si almacena una referencia a su UIView, también debe conservar esta UIView. Solo porque este es el estilo Cococa/Obj-C. – Felix