3

I stuck in a problem, checked out many answers for a few days. My problem is when i choose profile>Leaks in Xcode, i see UILabel(CALayer) is always getting bigger in Live Bytes. Is it normal or is it a leak? what can i do about it? It is getting bigger when i change class and back to class which has these labels. Its like they are allocating over and over again but not release old ones.

i set my labels in h. file like that

@property(weak,nonatomic)IBOutlet UILabel *lblNumbersSpelling1;
@property(weak,nonatomic)IBOutlet UILabel *lblNumbersSpelling2;
@property(weak,nonatomic)IBOutlet UILabel *lblNumbersSpelling3;
@property(weak,nonatomic)IBOutlet UILabel *lblNumbersSpelling4;

and ,

- (void)viewDidUnload
{
    [super viewDidUnload];

    self.lblNumbersSpelling1=nil;
    self.lblNumbersSpelling2=nil;
    self.lblNumbersSpelling3=nil;
    self.lblNumbersSpelling4=nil;
    self.lblRecordSayfasiNot=nil;
   // Release any retained subviews of the main view.
}

i did that and i m not sure if it is necessary what i did in viewDidUnload method. I use Xcode 5. Thanks in advance.

Yucel Bayram
  • 1,653
  • 2
  • 22
  • 41
  • you don't have to if you're using ARC, but additionally you need to release/reload when receive memory warning in your controller. – Jacek Grygiel Dec 02 '13 at 13:07
  • @fenk There is no warning, i cleaned them up but labels are getting bigger in instruments section of Xcode. I dont know if it is a leak or what. thanks for reply by the way. – Yucel Bayram Dec 02 '13 at 13:09
  • Are you using `ARC`? If so you don't need to do `self.lbl.... = nil;` If you're not using `ARC` then you don't do this in `viewDidUnload` it needs to be done in `dealloc` but I would recommend moving to using `ARC` Apple are really starting to push developers to use it also in my opinion it makes life a lot easier. – Popeye Dec 02 '13 at 13:36
  • @Popeye yes i do use ARC. – Yucel Bayram Dec 02 '13 at 13:43
  • No need to do that then. Your label objects will be release automatically this is the purpose of `ARC` it is an Automated Memory Management System. – Popeye Dec 02 '13 at 13:45
  • thank you, yes i agree about that but do you have an idea why it's look like getting bigger in profile? – Yucel Bayram Dec 02 '13 at 13:52

2 Answers2

2

viewDidUnload is deprecated in iOS6 and later.

Probably you want do this:

- (void)dealloc
{
    _lblNumbersSpelling1=nil;
    _lblNumbersSpelling2=nil;
    _lblNumbersSpelling3=nil;
    _lblNumbersSpelling4=nil;
    _lblRecordSayfasiNot=nil;
}
NSDmitry
  • 460
  • 5
  • 12
  • Thanks i m checking it out. It is weird Xcode does not give any warning about viewDidUnload method is deprecated!! – Yucel Bayram Dec 02 '13 at 13:29
  • 1
    As the user has now indicated that they are using `ARC` they will not be able to use `dealloc` as this never gets called when using `ARC`. @yucelbayram I've just checked using xcode 5 and you are right there is no deprecation warning even though it has definitely been deprecated. I also checked in a version of xcode before 5 and it does have the deprecation warning this could be a bug in xcode 5 but trust the docs that `ProFFeSSor` has provided it is definitely deprecated. – Popeye Dec 02 '13 at 13:48
  • Yes i have no doubt about that anymore, this method deprecated. thanks – Yucel Bayram Dec 02 '13 at 13:53
  • 5
    @Popeye dealloc certainly does get called under ARC. You can't call it yourself, or call super, but dealloc is still executed. – jrturton Dec 02 '13 at 13:54
  • According to Apple's documentation, "you shouldn’t use accessor methods to set an instance variable ... in initializer methods and dealloc." So consider rewriting `self.foo = nil` as `_foo = nil`. – jlehr Dec 02 '13 at 13:57
  • @jrturton I will stand corrected but I was under the impression that when using `ARC` although `dealloc` may get called in the background I can't go adding something like `- (void)dealloc { NSLog(@"dealloc"); self.lblNumbersSpelling1=nil; }` which is what I meant. Is that correct or could I still call this sort of thing because I have tried and the `NSLog` doesn't get printed. – Popeye Dec 02 '13 at 13:58
  • 5
    @Popeye Incorrect. You can, and in many situations *must*, override `dealloc` in ARC-compiled code. However `dealloc` implementations under ARC cannot call `[super dealloc]`. If your `dealloc` method isn't being executed, your object is probably leaked (or cached indefinitely). – jlehr Dec 02 '13 at 14:00
  • @jlehr that is definitely good to know I will review my own code now thanks. – Popeye Dec 02 '13 at 14:04
  • I'm sorry, forget about `[super dealloc]`. Thank you for your comment! – NSDmitry Dec 02 '13 at 14:05
  • 1
    Based on the chosen answer on http://stackoverflow.com/questions/14501115/do-i-need-use-dealloc-method-with-arc I would say there is no need for dealloc method at all in this case. – Popeye Dec 05 '13 at 08:43
2

I experienced the same problem with my application with UILabel(CALayer) increasing in memory usage in Xcode's profile tool. At the end of the day, UILabel(CALayer) increasing in memory ended up being a symptom of a memory leak caused by another issue (specifically a strong reference to a delegate).

I would check the following to make sure another is issue isn't causing the UILabel(CALayer) to be retained:

  1. Invalidate any NSTimers
  2. Remove any observers to NSNotificationCenter
  3. Make sure you are using weak references to self in blocks
  4. Make sure that any delegate properties use weak references

Source: http://www.reigndesign.com/blog/debugging-retain-cycles-in-objective-c-four-likely-culprits/

Ed-E G
  • 331
  • 5
  • 7
  • I had this situation in a few projects, I am sure about first and second. But third and fourth make sense. Thanks i will try this in next time and i ll let you know the result. – Yucel Bayram Jun 08 '15 at 06:17