Exception in dispose_all_Windows?

Hi everyone, lately I've been creating custom scenes and windows to show variables and other parameters.

but I have a problem, it turns out that I created a window in Scene_Map and this window is always on screen (on the map) because it refreshes constantly.

but, when calling another scene, this window disappears, and it is because by default in Scene_Base, it defines that when a scene ends, it deletes all the windows so that another scene is shown.
can be seen in "Scene_Base" line 61:

zxzzz436095.png



I was watching a little more and it turns out that the Scene_Base deletes all the variables that start with "Window" to massively delete the windows when the scene changes, you can see this in "Scene_Base" line 101:

zxzzz436094.png


Is there a way to make an exception so that a specific window so that it is not deleted in this process?
And in that case, can you make an exception more than one window?


I was reading the content manual, but it hasn't given me an answer.

in advance, I really appreciate the help.
Yeah and nope.

You see, everything is possible, but that's a terrible idea. Windows should always be disposed of!

You don't want your game to suffer from memory leaks and weird floating windows and sprites showing up where they shouldn't, right?
Yeah and nope.

You see, everything is possible, but that's a terrible idea. Windows should always be disposed of!

You don't want your game to suffer from memory leaks and weird floating windows and sprites showing up where they shouldn't, right?
Scene switching removes windows, but returning to the same scene makes them appear again.
I just want certain windows not to be removed in changing scenes.

In addition, each window has its individual "dispose" if one defines it, so there would be no problems in that mode.
there are many ways to hide windows to avoid consuming performance.

I just want an exception.
like this example:

Ruby:
 def dispose_all_windows
     instance_variables.each do |varname|
       ivar = instance_variable_get (varname)
       ivar.dispose if ivar.is_a? (Window) except Window_Example && Window_Example2
     end
   end
The best way to ensure there won't be a memory leak is to dispose of them. Any other strategy is risky.

There's nothing bad in defining the same window in 2 different scenes, no matter if they switch between them by pressing any button or not.

The only alternative would be to mix both scenes into a single one with extra conditions to define when a given window should show up or not.

An invisible window is still a window loaded into memory so not properly disposed of at the end of a scene is always a problem. Hiding it doesn't solve your issue, it'd only be a hack programmers should advice against.
I just want an exception.
like this example:

Ruby:
 def dispose_all_windows
     instance_variables.each do |varname|
       ivar = instance_variable_get (varname)
       ivar.dispose if ivar.is_a? (Window) except Window_Example && Window_Example2
     end
   end

I don't know much (if anything) about memory lag and it sounds scary (plus Kyonides seems to know his/her stuff), so maybe best not to do this if it'll muck things up...

But!

If you're just looking for how to write that line you used as an example in Ruby / RGSS3, then I think all you'd need to do is write it like this:

ivar.dispose if ivar.is_a?(Window) unless ivar == Window_Example || Window_Example2

Theoretically... I think.
Haven't tested it, haven't dabbled much in window disposal, but pretty sure that code should work.
I don't know much (if anything) about memory lag and it sounds scary (plus Kyonides seems to know his/her stuff), so maybe best not to do this if it'll muck things up...

But!

If you're just looking for how to write that line you used as an example in Ruby / RGSS3, then I think all you'd need to do is write it like this:

ivar.dispose if ivar.is_a?(Window) unless ivar == Window_Example || Window_Example2

Theoretically... I think.
Haven't tested it, haven't dabbled much in window disposal, but pretty sure that code should work.

I really appreciate the help, but it doesn't solve my problem...
that code only causes that when the windows "Window_Example" and "Window_Example2" are shown, command dispose_all_window does not delete any window in scene. and it's not the idea, the idea is for the command to delete all windows except the ones mentioned.
Here, I don't know how well you know your RGSS but here's a quick little hack.
Spoiler: Code
Ruby:
class Scene_Base
 
  # It's not pretty for neat code but this will let you carry the window
  # to every scene minus the scenes listed below. This will also retain
  # the dispose function otherwise you end up with nasty garbage collection.
  # You should always dispose your windows regardless.
 
  alias :xane_window_keeper_start :start
 
  def start
    xane_window_keeper_start
    create_my_keeper_windows
  end
 
  def create_my_keeper_windows
    return if SceneManager.scene_is?(Scene_Title)
    return if SceneManager.scene_is?(Scene_Battle)
    return if SceneManager.scene_is?(Scene_Save)
    return if SceneManager.scene_is?(Scene_Load)
    # You can refrence this window by using the variable @custom_window
    @custom_window = nil # Your window class name here.
  end
end
No matter what you should always dispose of your Window Classes. You'll create a bad memory leak otherwise and end up with a major headache down the line that you can't track down.
I really appreciate the help, but it doesn't solve my problem...
that code only causes that when the windows "Window_Example" and "Window_Example2" are shown, command dispose_all_window does not delete any window in scene. and it's not the idea, the idea is for the command to delete all windows except the ones mentioned.
Ah, I was worried that would happen.
Yeah sorry, out of my wheelhouse a bit for now then, thankfully others are here to help (like Xane and Kyonides).
Ah, I was worried that would happen.
Yeah sorry, out of my wheelhouse a bit for now then, thankfully others are here to help (like Xane and Kyonides).

Don't worry, also using the code that you have provided will be useful for other things, so thanks also c:
Here, I don't know how well you know your RGSS but here's a quick little hack.
Spoiler: Code
Ruby:
class Scene_Base

  # It's not pretty for neat code but this will let you carry the window
  # to every scene minus the scenes listed below. This will also retain
  # the dispose function otherwise you end up with nasty garbage collection.
  # You should always dispose your windows regardless.

  alias :xane_window_keeper_start :start

  def start
    xane_window_keeper_start
    create_my_keeper_windows
  end

  def create_my_keeper_windows
    return if SceneManager.scene_is?(Scene_Title)
    return if SceneManager.scene_is?(Scene_Battle)
    return if SceneManager.scene_is?(Scene_Save)
    return if SceneManager.scene_is?(Scene_Load)
    # You can refrence this window by using the variable @custom_window
    @custom_window = nil # Your window class name here.
  end
end
No matter what you should always dispose of your Window Classes. You'll create a bad memory leak otherwise and end up with a major headache down the line that you can't track down.

It works perfectly!
Thanks for the help! c:
There is one solution to avoid memory leak and keep the window.
The memory leak happens when there is no variable that references to the window.

The memory leak happens when you do this
Code:
@my_window = Window_Base.new(1,1,100,00)
@my_window = nil
Since it loses its reference, you will still able to see the window is there, but you can not access it. You can not dispose it. It stuck there in the game without you not being able to do something about it. This is why the Scene_Base take care of it.

Now, if you make an exception in dispose all window method, your window will lose its reference. It will create stacks of an undisposed window that loses its reference. Eventually, build up stack and memory leak that causes a crash. Please mind this before proceeding further.

However, the method to dispose all windows is not perfect.
if you store your windows in an array, the dispose all window method will not detect your window because the variable will be recognized as an array, not Window.

If you store your Window in a global variable, i.e,
Code:
$my_window = Window_Base.new(1,1,100,100)
It will not be disposed as well. And it will not create a memory leak as long as you never change the $my_window variable. You don't even need to change the dispose all window method if you're going this way.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/exception-in-dispose_all_windows.124877/