Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows are not released when main reference is deleted #1215

Closed
samschott opened this issue Mar 4, 2021 · 0 comments · Fixed by #2066
Closed

Windows are not released when main reference is deleted #1215

samschott opened this issue Mar 4, 2021 · 0 comments · Fixed by #2066
Labels
bug A crash or error in behavior. macOS The issue relates to Apple macOS support.

Comments

@samschott
Copy link
Member

samschott commented Mar 4, 2021

Describe the bug
When a window is instantiated in an app and the reference to that window is later deleted, the actual object is never garbage collected.

To Reproduce

Here is some example code which counts the number of Window instances:

import gc
import toga
from toga.constants import COLUMN
from toga.style import Pack


class Window(toga.Window):

    instances = 0

    def __init__(self):
        super().__init__(title=f'Window: {Window.instances}', position=(200, 200))
        Window.instances += 1

    def __del__(self):
        Window.instances -= 1


class RefcountExample(toga.App):

    def startup(self):

        self.window = None

        self.button_create = toga.Button(
            'Create window',
            style=Pack(padding=10, width=120),
            on_press=self.on_create,
        )

        self.button_delete = toga.Button(
            'Close and delete window',
            style=Pack(padding=10, width=120),
            on_press=self.on_delete,
        )

        self.label = toga.Label(
            f'Instances: {Window.instances}',
            style=Pack(padding=10, width=120),
        )

        self.button_create.enabled = True
        self.button_delete.enabled = False

        self.box = toga.Box(
            children=[
                self.button_create,
                self.button_delete,
                self.label,
            ],
            style=Pack(direction=COLUMN),
        )

        self.main_window = toga.MainWindow()
        self.main_window.content = self.box
        self.main_window.show()

    def on_create(self, sender):
        if not self.window:
            self.window = Window()
            self.app.windows += self.window
            self.window.content = toga.Box()
            self.window.show()
            self.label.text = f'Instances: {Window.instances}'

        self.button_create.enabled = False
        self.button_delete.enabled = True

    def on_delete(self, sender):
        if self.window:
            self.window.close()
            self.window = None
            gc.collect()  # clean up ref cycles before displaying instance count
            self.label.text = f'Instances: {Window.instances}'

        self.button_create.enabled = True
        self.button_delete.enabled = False


def main():
    return RefcountExample('Example', 'org.beeware.refcount')


if __name__ == '__main__':
    app = main()
    app.main_loop()

Expected behavior
Even if circular references to the window object exist, I would expect them to be garbage collected.

Environment:

  • Operating System: macOS
  • Python version: 3.9
  • Software versions:
    • Briefcase:
    • Toga: 0.3.0.dev27

Additional context
The implications could be worse than they initially seem since all window content will be retained as well and can potentially use a log of memory (imagine images or a video playing in the window),.

@samschott samschott added the bug A crash or error in behavior. label Mar 4, 2021
@freakboy3742 freakboy3742 added up-for-grabs macOS The issue relates to Apple macOS support. bug A crash or error in behavior. and removed bug A crash or error in behavior. labels Mar 29, 2022
@freakboy3742 freakboy3742 assigned mhsmith and samschott and unassigned mhsmith and samschott Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior. macOS The issue relates to Apple macOS support.
Projects
None yet
3 participants