Note that there are some explanatory texts on larger screens.

plurals
  1. POAm I using lists wrong? Memory leak in this simple python / pyglet code
    primarykey
    data
    text
    <p>I'm writing a simple 'avoid the falling enemies' type game in pyglet. The objects are generated above the screen, travel downward, and are destroyed when they pass below the visible screen. However, I'm doing something terribly wrong and the longer the program runs, the number of objects rises and rises. (It was slowing down and I discovered the problem with objgraph.) When I remove this class, the problem disappears.</p> <p>Falling Enemy class:</p> <pre><code>class Enemy(pyglet.sprite.Sprite): def __init__(self, **kwargs): super(Enemy, self).__init__(img=images.enemy_anim["front"], **kwargs) self.out_of_bounds = False def update(self, dt): self._move(dt) self._check_boundaries(dt) self._check_kill_condition() def _move(self, dt): self.y -= ENEMY_SPEED * dt def _check_boundaries(self, dt): if self.y + self.height &lt; 0: self.out_of_bounds = True def _check_kill_condition(self): if self.out_of_bounds: enemy_list.remove(self) self.delete </code></pre> <p>Main method code: </p> <pre><code>enemy_list = [] def add_enemy(*args, **kwargs): randx = random.randint(0, WINDOW_WIDTH - images.enemy_anim["front"].get_max_width()) randy = WINDOW_HEIGHT + images.enemy_anim["front"].get_max_height() enemy_list.append(Enemy(x=randx, y=randy, batch=update_batch)) def update(dt): for sprite in enemy_list: sprite.update(dt) #send the above functions to the pyglet scheduler pyglet.clock.schedule_interval(update, 1/TICKS_PER_SECOND) pyglet.clock.schedule_interval(add_enemy, 1/ENEMY_ADD_RATE) </code></pre> <p>I have my enemy animation stored in a separate module (images) in a dictionary (images.enemy_anim).</p> <p>I create an enemy with the reference inside a list.</p> <p>When the enemy dies, I tell it to remove itself from the list. I've checked the length of the list during runtime and it's always the appropriate length. So the list isn't growing out of control. The object should have no references after it's removed from the list, right?</p> <p>What am I doing wrong?</p> <p>EDIT:</p> <p>The problem was that I had <code>self.delete</code> instead of <code>self.delete()</code>. Stupid mistake...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload