I'm having trouble disposing an image that is in a loop.
def draw_target_image @target_arrow.dispose if @target_arrow BattleManager.ctb_order.each_with_index do |i, index| if i[:name] == enemy.name if enemy.index @target_arrow = Sprite.new @target_arrow.bitmap = Cache.system "Target" @target_arrow.x = ((50 * index) + 6) + 274 @target_arrow.y = 380 @target_arrow.z = 100 end end endWhen I set @target_arrow.dispose, it only kills the last one made and the others stay in place.
How can I get them all to dispose?
Disposing An Image In A Loop
● ARCHIVED · READ-ONLY
-
-
Wild guess: You only dispose one. The dispose is not inside the loop. You create multiple sprites and assign them to the same variable but you only dispose one of those instances assigned to @target_arrow.dispose.
Edit: Better explanation perhaps:
- You dispose the @target_arrow variable (if any)
- Then the loop may create 10 sprites or so?
- You assign it 10x to @target_arrow variable (overwriting the previous assignment without disposing it first!).
- You now have a memory leak and sprites that are still in memory but with no reference (probably partially collected by the GC randomly at some point). -
So I put the sprites in an array and dispose each of them when needed. That seemed to work out better. I didn't know there was no way of accessing the older sprites.
This is solved now. :D