40

So, I have class:

@interface Controller : NSObject
{
    UILabel* fileDescription;
}

@property(strong, nonatomic) UILabel* fileDescription;

Do I need use method dealloc where property fileDescription will equal nil?
For example:

-(void)dealloc
{
    fileDescription = nil;
}

If not, who will dismiss memory used by fileDescription?

johankj
  • 1,765
  • 16
  • 34
Vit
  • 936
  • 1
  • 10
  • 20
  • **To answer title: `No`**, even if calling `alloc` manually (like many libraries), our `ARC` enabled App has not a single `dealloc` call (but has many `dealloc` implementations). **To answer what was meant: `Yes`**, the `dealloc` special-method (aka callback/hook/event/action/handler) is required to release/free dynamic-resources (anything not managed by `ARC`). – Top-Master Oct 20 '21 at 16:35

5 Answers5

81

Generally you don't need to provide a subclassed dealloc method as ARC manages the lifetime of the instance variables.

However it can be useful to perform clean-up other than releasing objects, for example to remove an observer or close a network connection down cleanly. You are therefore allowed to subclass dealloc under ARC, but you are not allowed to call [super dealloc] from within the subclassed method.

In your particular case it's not required, however.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
9

No.

You don't need dealloc method in ARC.

But if you want to do some cleanup tasks when your view is dismissing or released. It's the best place, In such case you can implement it.

For example:

You are running a timer in your view and it's updating your view. When you are dismissed the view you need to stop that timer. In that condition you can use dealloc method and stop timer there.

Similar for NSNotification observer.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • 6
    Stopping a timer in `dealloc` it is a bit tricky, because timers retain their target. So if a view controller starts a timer with `target:self`, it will not be deallocated as long as the timer is running. (Compare http://stackoverflow.com/questions/14490178/how-to-know-when-to-invalidate-an-nstimer/14490927#14490927.) – Martin R Jan 24 '13 at 12:27
  • 1
    @MartinR: Thanks for your comment :) And the valuable information :) – Midhun MP Jan 24 '13 at 12:32
  • Should just use viewDidDisappear, or viewWillDisappear – shim May 09 '14 at 23:06
  • @shim: No you can't use those, Hint: it will be called each time the view is disappeared from the screen. – Midhun MP May 10 '14 at 14:04
  • For the example that Midhun MP has given of deactivating a timer upon dismissing a view, I don't see why not. – shim May 10 '14 at 14:16
  • @shim: Ok, I said dismissing the view, what happens if you present a view controller from that view ? It will also trigger the methods you mentioned above. – Midhun MP May 10 '14 at 14:19
  • @shim: Why should I ? It is far better to use dealloc method in this case, instead of using `isMovingFromParentViewController` – Midhun MP May 10 '14 at 17:13
  • @shim: that only applicable for NSTimer, for other things like observers it is better to use dealloc – Midhun MP May 10 '14 at 17:23
  • NSTimer is what was being discussed. – shim May 10 '14 at 17:29
  • @shim: No, not at all. Check the question before leaving such comments **Do I need use dealloc method with ARC?** – Midhun MP May 10 '14 at 17:30
  • Your answer gives the example of running a timer, someone commented that that wouldn't work, and I elaborated upon it. There's no need to be condescending. – shim May 10 '14 at 19:11
6

If you are using ARC.

No need to use dealloc and release, compiler knows that your property and objects are strong / weak so it will manage it.

EDIT:

dealloc method is required if you use coreframework objects like CG... & CF.... Even you create observers for notifications you need to remove it and dealloc is the best place to removeObserver.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

Ans is NO Because with ARC no need to dealloc.

yen
  • 772
  • 3
  • 10
  • 3
    This answer is too general. It sounds like there is never a reason to call the dealloc method (sometimes there is, as noted in other answers). – borrrden Apr 25 '14 at 01:33
0

As you are using ARC you don't have to use dealloc Complier will set the autoreleasePool depending upon the scope of the property,variable or control. And it'll will release the memory. There are different types of autoreleasepool generally we can define them as function level,class level etc etc. Hope this helps.

Rushi
  • 4,553
  • 4
  • 33
  • 46