Game automatically closes when a specific part of code runs (code included in the post)

Hello community. The problem I am experiencing so far is as the title says: when the code below runs, after a while the game closes.
I get no error message and the code works perfectly until it closes...and when it closes I get no error messages at all, it just closes the game as if I had pressed Alt+F4.

Spoiler
Code:
class Window_Exercise < Window_Base
  def initialize(exercise_window, x, y, width, height, numbers, plusminus)
    super(x, y, width, height)
    @numbers = numbers
    @plusminus = plusminus
    @exercise_window = exercise_window
    refresh
  end
  
  def draw_images
    pic = Array.new
    src = Array.new
    ret = Array.new
    for i in 0..1
      pic << Bitmap.new(@numbers[i].image)
      src << Rect.new(0,0,pic[i].width,pic[i].height)
      ret << Rect.new(174*i,0,pic[i].width,pic[i].height)
      self.contents.stretch_blt(ret[i],pic[i],src[i])
    end
#-------------------------------------------------------------------------------
    i+=1
    pic << Bitmap.new(@result_num.image)
    src << Rect.new(0,0,pic[i].width,pic[i].height)
    ret << Rect.new(174*i,0,pic[i].width,pic[i].height)
    self.contents.stretch_blt(ret[i],pic[i],src[i])
#-------------------------------------------------------------------------------
    i+=1
    pic << Bitmap.new("Graphics/Pictures/#{@plusminus}")
    src << Rect.new(0,0,pic[i].width,pic[i].height)
    ret << Rect.new(116,0,pic[i].width,pic[i].height)
    self.contents.stretch_blt(ret[i],pic[i],src[i])
    i+=1
    pic << Bitmap.new("Graphics/Pictures/=")
    src << Rect.new(0,0,pic[i].width,pic[i].height)
    ret << Rect.new(280,0,pic[i].width,pic[i].height)
    self.contents.stretch_blt(ret[i],pic[i],src[i])
  end
  
  def update
    super
    refresh
  end
  
  def refresh
    contents.clear
    @result_num = @exercise_window.item
    draw_images
  end
end
I don't have a solution, but two tips for searching for the cause.


1) have the task manager open while testing, and check if there is any indication of a memory overflow or something like that


2) add print commands for console output to see the last actions before the crash
Well I have a few tips to improve the code:


1: You are redrawing the images to much from what I can gather. I would say only call the refresh method if your image data is going to change.


2: All three of your arrays: pic, src and ret would be better off as instance variables created at initialization like so:

Code:
def initialize(exercise_window, x, y, width, height, numbers, plusminus)
    super(x, y, width, height)
    @numbers = numbers
    @plusminus = plusminus
    @exercise_window = exercise_window
    @pic = []
    @src = []
    @ret = []
    refresh
  end

3: it appears to me that your i variable is being used outside the scope of your for...loop.


Anyway not sure if this will fix your problem but should help improve the code.
Thank you all for your answers. I tried what Andar said and there is no memory overflowing. I'll modify the code later to avoid refreshing images so often too. What I am really surprised about is that this code doesn't crash at all...as you can see there is only 1 difference...


Am I missing something? I just tried to increase a variable because I wanted to print it and then it stopped crashing...😳

Spoiler
Code:
class Window_Exercise < Window_Base
  def initialize(exercise_window, x, y, width, height, numbers, plusminus)
    super(x, y, width, height)
    @numbers = numbers
    @plusminus = plusminus
    @exercise_window = exercise_window
    refresh
  end
  
  def draw_images
    pic = Array.new
    src = Array.new
    ret = Array.new
    for i in 0..1
      pic << Bitmap.new(@numbers[i].image)
      src << Rect.new(0,0,pic[i].width,pic[i].height)
      ret << Rect.new(174*i,0,pic[i].width,pic[i].height)
      self.contents.stretch_blt(ret[i],pic[i],src[i])
      print " img #{i}"
    end
#-------------------------------------------------------------------------------
    i+=1
    pic << Bitmap.new(@result_num.image)
    src << Rect.new(0,0,pic[i].width,pic[i].height)
    ret << Rect.new(174*i,0,pic[i].width,pic[i].height)
    self.contents.stretch_blt(ret[i],pic[i],src[i])
#-------------------------------------------------------------------------------
    i+=1
    pic << Bitmap.new("Graphics/Pictures/#{@plusminus}")
    src << Rect.new(0,0,pic[i].width,pic[i].height)
    ret << Rect.new(116,0,pic[i].width,pic[i].height)
    self.contents.stretch_blt(ret[i],pic[i],src[i])
    i+=1
    pic << Bitmap.new("Graphics/Pictures/=")
    src << Rect.new(0,0,pic[i].width,pic[i].height)
    ret << Rect.new(280,0,pic[i].width,pic[i].height)
    self.contents.stretch_blt(ret[i],pic[i],src[i])
  end
  
  def update
    super
    refresh
  end
  
  def refresh
    contents.clear
    @result_num = @exercise_window.item
    draw_images
    $game_variables[5]+=1
  end
end
Check to see if any script is using $game_variables[5] (that's the variable you're talking about right?). It could be possible that this is not a crash at all but a forced shutdown if your "crash" has stopped by increasing that variable.
I am testing it in a fresh project, there is no script but that one at the moment.


EDIT:


Calling the refresh method only when the image changes apparently solved the problem. I am not sure it won't crash if you change images too often but honestly based on the time needed to crash I think you should update images manually for few hours without doing anything else.


I would say that this solved my problem =) Thank you all!


P.S. I would still like to know why that $game_variables[5]+= 1 prevented the game to crash...

Original thread: https://forums.rpgmakerweb.com/threads/game-automatically-closes-when-a-specific-part-of-code-runs-code-included-in-the-post.69810/