0
votes

I have a UILabel in my class header file defined as :

 `@property (nonatomic, retain) UILabel *label1;` 

and it exists as instance variable like this:

 `UILabel *label1;` 

and synthesized in the .m file, however, in viewDidLoad method I do:

 `label1 = [UILabel alloc] init] autorelease];`

then I do various things on the label like setting its frame, text color, etc ... when the view controller is deallocated, the app crashes with this message in console

 (Zombies enabled): `[CALayer release] message sent to deallocated instance` ...

The app will not crash when I :

1) remove the autorelease word .. or

2) if i do not release label1 in the dealloc method .. or

3) remove [super dealloc]; from the dealloc method of the view controller.

how can I properly release this UILabel without facing such crash !!

5
the label is released already before dealloc is called. that is because its an autorelease object. your dealloc is trying to release a uilabel that already been released, an its crash.. in your question. you can use 1 or 2.janusfidel
but it is already retained in the @property line in the .h header file....JAHelia
if i use one of the 3 points above, the app will certainly leak memoryJAHelia
Allocing will increase its retain count, then when it's autoreleased its reduced, then you're releasing a dealloc'd object. Exactly as janusfidel said. No need to have it as a property, its an autorelease view object, only needs a pointerElmo
you allocated the label just once, that is in your viewDidLoad, @property(retain) will not allocate anything, but will tell the compiler how we want our properties treated.janusfidel

5 Answers

2
votes

You are doing right.Autorelease and release in dealloc. But it shouldn't be crash.Because I did the same thing to check. Could you please check accciendlty may be u release the label some where else. And releasing in dealloc again.

1
votes

since you have declared the label as retain. The allocation can be

UILabel *myLabel = [[UILabel alloc] init];
// set all properties of label
self.label1 = myLabel;
[myLabel release];
myLabel = nil;

And in dealloc release your label1.

[label1 release];

this is the way I'm used to and this makes things smoother for me.

0
votes

the label is released already before dealloc is called. that is because its an autorelease object. your dealloc is trying to release a UIlabel that already been released, an it crashes.. in your question. you can use 1 or 2. if you allocated the object once, then call a release just once. its not because you assign retain to your property in @property directive will add 1 retain count to your object , @property(retain) will not allocate anything, but will tell the compiler how you want your properties treated

0
votes

strangely enough, when I used self.label1 = [[[UILabel alloc] init]autorelease]; instead of label1 = [[[UILabel alloc] init] autorelease]; solved the problem. the dealloc method remains as is without any change. really weird !!

0
votes

Do this and u will not use autorelease for label1:

 - (void)dealloc
{
  if(label1)
  {
    label1 = nil;
    [label1 release];
  }
  [super dealloc];
}