Memory Leak Issue: Scene Interpreter

An issue with the scene interpreter was reported 6 months ago by a member of the RPGMakerWeb community, Sixth, in the comments section for Scene Interpreter. Unfortunately, the report went unnoticed on my part and the issue continued to persist.

I have implemented a fix offered by Mithran for now. If possible, please update the script. I apologize for the inconvenience this may have caused for anyone that has used this script in their releases.

Below is an explanation of the problem and how it (probably) happened, as I don’t exactly remember the decisions that were being made when I originally wrote the script.

The Message Problem

It all begins with the creation of message windows.

The scene interpreter’s role is to allow you to basically run common events in any scene by creating a comment and writing

<run scene: current>

So for example, let’s say you used an item from the item menu, and the item called a common event. By default, the item menu would close and you would go back to the map, where the common event would execute. By using this script, the common event would execute directly in the item menu.

Most event commands work fine, but a number of commands did not because they relied on things that may not exist in the current scene. In particular, messages weren’t shown in scenes because there just wasn’t a message window available for the scene.

Introducing a “Scene Message Window”

My solution was to create a message window for every scene. Seems simple enough: whenever a scene starts up, just create a message window. Now you can just run your common event, and if text needs to be displayed, the scene’s message window will go ahead and read it.

Duplicate Messages

One problem I ran into was that when you showed messages on the map, a duplicate message would be shown. So you would run an event, and a message would be displayed. Once you close the message window, you’ll see another message window underneath it.

I then realized two message windows were being created: one of them was the “scene message window”, and the other is the default message window that the map was creating already.

I think what happened here is I decided to simply define the message window the same way as the ones found in Scene_Map and Scene_Battle

def create_message_window
  @message_window = Window_SceneMessage.new
end

So our message windows are still being created twice, but the second time it’s created, it will simply replace the old one and carry on with that.

Memory Leak

Of course, that assumption was wrong. While the reference to the old message window was lost, it was not properly disposed. Over-time, more and more of these old message windows started to accumulate, consuming more memory.

Eventually, as some developers have found out, the performance of their game became very sluggish over-time since the game could not get rid of all these message windows.

Fixing the Leak

At this point, I have not figured out a proper way to address the issue. The solution offered by Mithran was to avoid replacing the reference:

class Scene_Map
  def create_message_window
    @message_window = Window_Message.new unless @message_window && !@message_window.disposed?
  end
end

class Scene_Battle
  def create_message_window
    @message_window = Window_Message.new unless @message_window && !@message_window.disposed?
  end
end

This would take care of the issue for the map scene and battle scene, but if you have any other scenes that are also creating message windows, the issue would occur again.

Evidently, the decision to resolve the duplicate message window issue by simply “replacing” it and hoping the game would clean up the reference was wrong and the consequences are devastating.

 

You may also like...

28 Responses

  1. fynmorph says:

    Just in case you don’t see my post on rpgmaker.net

    I think i found a bug. When you call an event that changes a stat (say, remove 1 HP), it doesn’t refresh properly, it’s always 1 step back (and when you leave the menu and open again, it’s properly displayed now).

    Here’s a demo: http://puu.sh/kVKlS/6791692c00.rar
    Use the potion (it adds +1 max HP without script and remove 1 HP with script call).

  2. Sixth says:

    Ahoy!
    Just hopped in to say, that the fix itself was not made by me, I just reported the issue.
    I don’t use that script, but one of my friend uses it, and I saw the issue in his game.
    Mithran was the one who wrote the fix I posted originally.
    Good to see that the script is updated!

Leave a Reply

Your email address will not be published. Required fields are marked *