thebaselab / codeapp

Building a full-fledged code editor for iPad
https://code.thebaselab.com
MIT License
2.96k stars 203 forks source link

Memory leak in PDF extension #673

Open bummoblizard opened 1 year ago

bummoblizard commented 1 year ago

There seems to be a memory leak in PDF previews.

bummoblizard commented 1 year ago
image
bummoblizard commented 1 year ago

Same issue in markdown previews.

bummoblizard commented 1 year ago
private class Storage: ObservableObject {
    @Published var isLoading: Bool = true

    let pdfView = PDFView()

    deinit {
        print("PDFStorage Deinit")
    }
}

Deinit is never called.

bummoblizard commented 1 year ago

By storing a PDFView instance in an environment object (MainApp.editors), we are creating a retain cycle between the view and the object. That's why it's never released.

See Scenario 4 in this article: https://tiagolopes.blog/2022/11/01/when-does-a-swiftui-environment-get-retained/

bummoblizard commented 1 year ago

Multiple scenes also leaks the ObservableObjects including all extensions. Not sure what can be done about it.

bummoblizard commented 1 year ago

I think it's about the self cpaturing closures that we use a lot in MainApp. I'll look into it.

bummoblizard commented 1 year ago

I think it's about the self cpaturing closures that we use a lot in MainApp. I'll look into it.

It will take quite a lot of efforts to resolve this. Let's fix it in 1.4.1.