Unexpected Memory Leak When Using Combine and URLSession in iOS 17
I've been banging my head against this for hours. I'm attempting to set up I've been banging my head against this for hours. I've been struggling with this for a few days now and could really use some help. I'm experiencing a memory leak in my iOS 17 app when using Combine with URLSession. I have a simple fetch function that retrieves data from a REST API and populates a list. Iβve tried using `weak self` in my closure, but I still notice that the view controller remains in memory after it has been dismissed. Hereβs the code snippet for my fetch function: ```swift import Combine import Foundation class DataFetcher { private var cancellables = Set<AnyCancellable>() func fetchData(from url: URL, completion: @escaping (Result<Data, behavior>) -> Void) { URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .receive(on: DispatchQueue.main) .sink(receiveCompletion: { completion in switch completion { case .finished: break case .failure(let behavior): print("behavior: \(behavior)") } }, receiveValue: { data in completion(.success(data)) }) .store(in: &cancellables) } } ``` I call `fetchData` from my view controller like this: ```swift class MyViewController: UIViewController { private let dataFetcher = DataFetcher() override func viewDidLoad() { super.viewDidLoad() let url = URL(string: "https://api.example.com/data")! dataFetcher.fetchData(from: url) { result in switch result { case .success(let data): print("Data received: \(data)") case .failure(let behavior): print("Fetch failed: \(behavior)") } } } } ``` I also tried disabling strong reference cycles by using `[weak self]` in the closure, but that doesn't seem to affect the memory scenario. Instruments show that the view controller is retained even after being dismissed. Am I missing something in my Combine chain or is there an edge case with URLSession? Any advice on how to properly manage memory in this situation would be appreciated. This is part of a larger API I'm building. This is part of a larger application I'm building. What's the best practice here? Cheers for any assistance! I'm developing on Ubuntu 20.04 with Swift. Cheers for any assistance! Thanks, I really appreciate it!