0

I'm new to Swift's ARC and I'm having a possibly memory problem with the following swift code:

class Device {
    let name: String
    var ports: [Port]

    init(name: String, ports: [Port] = []) {
        self.name = name
        self.ports = ports
     }
}


class Port {
    let name: String
    let device: Device

    init(name: String, device: Device) {
        self.name = name
        self.device = device
     }
}

In the provided Swift code, a device object contains an array of ports, and each port object is associated with a specific device. A device should remain in memory as long as it's ports exist and the device of a port should never be nil. Sometimes the ports should only stay in the device's ports array, without any other reference. But with the code above, unused instances will stay in memory for ever and getting more and more.

I've already tested the following:

1.) weak let device: Device? in the Port class. That way i can't do something like this anymore, because the Device is getting deallocated immediately (after view change):

let device = Device(name: "name")
let port = Port(name: "port", device: device)

// change view to PortView with port object / without device object --> device get's deinitalized.

2.) Weak Array (with a property wrapper). But if the ports in the array are weak, they also getting deinitalized immediately when there is no other reference to it (logically) - but sometimes there is intentionally no reference to them, so they have to stay in the array.

Where is my mistake in thinking?

5
  • I'd use one, but the whole logic is how you need to keep reference on the parent (the Device instance that holds the ports). You could also use struct, or explain why Port needs to know its device? As in you sample, from device you can't get the port. Commented Nov 6, 2023 at 19:01
  • This example is only intended to illustrate it in a simplified manner, in my project it is a bit larger and unfortunately classes are necessary (due to inheritance, etc.). Depending on the view (e.g. PortView), only the respective port object should be passed - but details of the device should also be displayed there (e.g port.getDevice().getName()). In my port class there are also many methods that executes API calls which requires the name of the respective device. Commented Nov 6, 2023 at 19:27
  • 1
    "but sometimes there is intentionally no reference to them, so they have to stay in the array." OK, so when do you want the port and/or device to be deallocated? It sounds like you're relying on the fact that they're self-retaining. In that case, you'd need an explicit "destroy" method (something that would nil-out everything to break the retain cycles). Otherwise, you need something that holds onto all the devices or ports. (That shouldn't be the View; that's transitory. The View should just show the current state of a Model object is. It shouldn't own this data.) Commented Nov 6, 2023 at 19:44
  • 2
    There's no magic here. It's up to you to decide when these things should exist (in which case you need to maintain a reference to them) and when they aren't needed (in which case you should clear your reference to them). I suspect your mistake is that you're trying to have the View hold everything, instead of pulling it out into a Model. developer.apple.com/documentation/swiftui/… Commented Nov 6, 2023 at 19:46
  • Thanks a lot. I haven't had much to do with memory management before, so I hadn't given it much thought until now. In the end it was a lot easier than I thought. I already have a model in which an array holds all the devices with its ports. Previously, during a refresh, this array was simply overwritten with the newly returned devices. But if I empty the ports array for all devices beforehand, everything will be properly deallocated. Commented Nov 12, 2023 at 20:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.