0

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 !!

JAHelia
  • 6,934
  • 17
  • 74
  • 134
  • 1
    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 Jul 24 '12 at 10:47
  • but it is already retained in the @property line in the .h header file.... – JAHelia Jul 24 '12 at 10:49
  • if i use one of the 3 points above, the app will certainly leak memory – JAHelia Jul 24 '12 at 10:52
  • 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 pointer – Elmo Jul 24 '12 at 10:52
  • i need it to be a property because i need to use it in various parts in my class. – JAHelia Jul 24 '12 at 10:54
  • I think you are misunderstanding the property attribute in objc. passing a retain in your property setter does not require you to release the object twice. http://stackoverflow.com/questions/9859719/xcode-property-attributes-nonatomic-copy-strong-weak – janusfidel Jul 24 '12 at 10:59
  • @ janusfidel I know that, but the label1 is retained in the @propery (+1 retain count) and allocated in viewDidLoad (+1 retain count), so what does that mean !!!? – JAHelia Jul 24 '12 at 11:01
  • 2
    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 Jul 24 '12 at 11:09
  • If you don't need to support iOS 3.x or lower, you might want to consider using ARC (Automatic Reference Counting). – Andreas Ley Jul 24 '12 at 11:28

5 Answers5

2

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.

kumar123
  • 791
  • 7
  • 21
1

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.

manileo86
  • 153
  • 7
0

Do this and u will not use autorelease for label1:

 - (void)dealloc
{
  if(label1)
  {
    label1 = nil;
    [label1 release];
  }
  [super dealloc];
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

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

janusfidel
  • 8,036
  • 4
  • 30
  • 53
0

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 !!

JAHelia
  • 6,934
  • 17
  • 74
  • 134
  • Yes It will work.But on your question you have mentioned that you are using self.label1 not label1.If you use self.label1 then you have to make the autorelease otherwise retina count of label1 become 2. – kumar123 Jul 24 '12 at 12:19
  • 1
    basically if you set a object reference directly it bypasses the setter (and thus the retain), i.e. label1=, if you use the property form self.label1= it calls the setter (and thus the retain). – ahwulf Jul 24 '12 at 14:41