pointfreeco / swift-perception

Observable tools, backported.
MIT License
575 stars 44 forks source link

Memory leak of Perceptible objects #51

Closed beforeold closed 8 months ago

beforeold commented 8 months ago

Description

The model won't deinit when the view dismissed.

seems the WithPerceptionTracking will capture the view and the model through the withPerceptionTracking function

Checklist

Expected behavior

the model deinitialzed after the view dismissed

Actual behavior

the model is alive

Steps to reproduce

  1. add a new @State property to Content of the example

    @State private var showsContentSheet = false
  2. add a sheet modifier to the body of the ContentView

    .sheet(isPresented: $showsContentSheet) {
      ContentView(model: .init())
    }
  3. add a button to the Form of the ContentView to show the sheet with a brand new ContentView

    Button("Present new ContentView") {
          self.showsContentSheet = true
        }
  4. add a deinit log for the model the CounterModel

    deinit {
    print(self, "deinit")
    }
  5. Run the example, Tap the Present new ContentView button, and drag to dismiss the new ContentView expectation falure for the deinit log

Perception version information

db087559dcc50eedebdc7df6a0fb0c1e188e4229

Destination operating system

iOS 16.7.3

Xcode version information

Xcode 15.0.1

Swift Compiler version information

swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
Target: arm64-apple-macosx14.0

Here is the demo project: this repo.

mbrandonw commented 8 months ago

Hi @beforeold, can you please provide a project that demonstrates the problem?

beforeold commented 8 months ago

Hi @beforeold, can you please provide a project that demonstrates the problem?

Hi @mbrandonw Thanks for your reply. Here is the exmaple. Please checkout the PerceptionExampleView in this repo.

mbrandonw commented 8 months ago

Thank you for the project, that helps. I can definitely see the issue. I imagine we will need to make use of some of the internal withObservationTracking functions so that we can explicitly cancel observation. We will look into this sometime soon, but we do have some travel planned currently and so we are not sure when.

beforeold commented 8 months ago

Cool, Very glad to hear there is a possible solution. Let me know if I can help.

Wish you have a nice trip.