python - Tkinter Canvas Update Memory Leak -


i'm seeing memory leak in code below. i'm creating , processing data in separate modules, isn't causing leak can see. believe it's because calling new instance of drawing class each time change scale, although i'm not sure how correct issue. i've read this thread, when try , implement self.canvas.destroy() method on code receive error. wondering method applied code below solve issue?

code snippet:

from tkinter import *  class interface_on:      def interface_elements(self, master):         self.master=master         self.master.title( "my canvas")         self.c=canvas(self.master, width=1000, height=1000, bg='black')         self.c.grid(row=0, column=0)         menubar = menu(master)         filemenu = menu(menubar, tearoff=0)         filemenu.add_command(label="new", command=self.edit_new)         menubar.add_cascade(label="file", menu=filemenu)         master.config(menu=menubar)         drawing_utility_run=drawing_utility()         drawing_utility_run.drawer(self.c)      def edit_new(self):          export_poscar = self.export_poscar = toplevel(self.master)         self.export_poscar.title('new ribbon...')         self.export_poscar.geometry('300x400')         self.scale_label= label(export_poscar, width=15, text='scale:')         self.scale_label.grid(row=2, column=0)         self.scale_label= label(export_poscar, width=15, text='scale:')         scale_var = stringvar()         self.scale_spin= spinbox(export_poscar, from_=1, to=1000, increment=1, width=15, command=self.execute_request_immediate, textvariable=scale_var)         self.scale_spin.grid(row=2, column=2)      def execute_request_immediate(self):         global scale         user_set_scale=float(self.scale_spin.get())         scale=user_set_scale         drawing_utility_run=drawing_utility()         drawing_utility_run.drawer(self.c)  class drawing_utility:     def drawer(self, canvas):         self.canvas=canvas         self.canvas.delete('all')         import generator #generates data (imports 'scale' above possible)         generator_run=generator.generator()         generator_run.generator_go()                 generator import coordinates_x_comp, coordinates_y_comp #imports necessary lists         import processor #imports coordinates_x_comp, coordinates_y_comp, cleans , analyses         process_xy_data=processor.data_processor()         process_xy_data.process_data()         processor import p_1, p_2         line in xrange(len(p_1)):             self.canvas.create_line(p_1[line],p_2[line], fill='red', activefill='blue', width=1)  root=tk() run_it_canvas=interface_on() run_it_canvas.interface_elements(root) root.mainloop() 

i'm not sure of these things fix memory leak you're observing, there few issues i'd fix might help:

1. you're using local variable (drawing_utility_run) store drawing_utility instances. it's not entirely clear why instances aren't getting garbage collected once method created in exits, either way, seems want object persist, should store reference in instance namespace, this:

self.drawing_utility_run=drawing_utility() self.drawing_utility_run.drawer(self.c) 

2. when delete canvas objects self.canvas.delete('all') you're relying on fact tkinter version implements string 'all' recognized constant, may case, isn't guaranteed. canvas.delete function accept argument, whether represents recognized constant or tag/id or not, without throwing error - example try self.canvas.delete('blah blah blah'). i.e. you're banking on self.canvas.delete('all') deleting objects, it's not obvious me so. use tkinter constant all instead of string 'all'.

3. unless have reason having imported modules exist in drawing_utility instance namespace, should move import statements top, in module-level namespace.

4. import statements redundant:

import generator #generates data (imports 'scale' above possible) generator import coordinates_x_comp, coordinates_y_comp #imports necessary lists import processor #imports coordinates_x_comp, coordinates_y_comp, cleans , analyses processor import p_1, p_2 

you don't need both import generator , from generator import coordinates_x_comp. import generator refer generator.coordinates_x_comp. using both import statements, you're double-importing generator.coordinates_x_comp, processor.p_1 etc.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -