What is the primary memory management issue with the following Swift networking code?
iOS interview question for Intermediate practice.
Answer
The completion handler captures self strongly, creating a potential strong reference cycle that causes a memory leak.
Explanation
The code creates a strong reference cycle. The ViewController instance owns the session, which creates the task. The task's completion handler needs to access a method on self (handleData), so it captures a strong reference to self. If the ViewController is dismissed before the network request completes, it cannot be deallocated because the task's completion handler still holds a strong reference to it. This memory leak can be fixed by using a weak capture list: [weak self].