Reputation: 61
I have just started learning memory management and I have some questions.
I am implementing a custom camera
var captureSession = AVCaptureSession()
Do I need to put a weak reference (weak var captureSession = AVCaptureSession()
) to this or will it get deallocated automatically once I move to another view controller which is not using the above resource?
I am currently stopping the captureSession in viewDidDisappear
What benefit will I get by adding weak self
in the following task?
URLSession.shared.dataTask(with: url!) { [weak self](data, response, error) in {
}
I usually add various observers to textfield, network check
and remove them in viewDidDisappear
. Do they get completely deallocated from memory or do I have to do something extra above this?
Upvotes: 0
Views: 135
Reputation: 3271
1. AVCaptureSession:
Don't set weak to your captureSession
variable, as there is no guarantee. Because your captureSession
would be deallocated at anytime when It's not in use. Also I'll recommend you to stop your captureSession
in viewWillDisappear
function.
Learn more about weak, Strong etc here:
2. weak self
in block:
When your completion block is holding by some other objects, you should avoid Strong reference cycle. So you should use weak self
for such completion blocks.
Please refer this post for more info.
3. Observers:
If you forgot to do removeObserver
to your class, your class will hold that observer even if you re init that same class.
Ex:
If you forgot to remove removeObserver
in your ViewController, when re init that same controller, your observer will be added again. So that observer method will be called twice and so on.
But removing observer is good enough and there is no need to release your textField or other objects, If you are using ARC.
Upvotes: 1