205 lines
7.3 KiB
Ruby
205 lines
7.3 KiB
Ruby
#==============================================================================
|
|
# LonaCraft - 02_ViewportGuard.rb
|
|
# What it does:
|
|
# * Keeps a Sprite, Plane, Window or Tilemap from outliving its Viewport.
|
|
# Why (2026-10-07, four Windows crash dumps of Game.exe): an access
|
|
# violation in RGSS301.dll+0x111a1c on the first draw of the crafting
|
|
# screen, inside Graphics.transition, in the engine's search of a
|
|
# viewport's list of drawables while a drawable is freed. The list
|
|
# belonged to a Viewport already disposed: some drawable on it was never
|
|
# disposed, and when the garbage collector frees that drawable (one runs
|
|
# at the scene transition) the engine unlinks it from the dead viewport's
|
|
# list. It crashes only when that memory was reused since (4 in ~15).
|
|
# * LonaCraft::ViewportGuard keeps {viewport id => {drawable id => [class
|
|
# name, made at]}} and {drawable id => viewport id}: object ids only, no
|
|
# references, so nothing is kept alive. A drawable is tracked when made
|
|
# with a viewport (Sprite / Plane / Tilemap.new(viewport)) or given one
|
|
# (viewport= on all four); dispose untracks it; viewport = nil untracks.
|
|
# * Viewport#dispose first disposes every tracked drawable still alive on
|
|
# it with the engine's own dispose (not the subclass's: its bitmaps and
|
|
# child objects are not touched), then disposes the viewport. Each one
|
|
# gives the line
|
|
# viewport guard: <class> still alive on a disposed Viewport, made at <where>; disposed it
|
|
# in the crash trace (05_Config.rb LonaCraft.trace, debug and trace on)
|
|
# and, once per class per game run, in the console.
|
|
# * "made at": with the trace on, four lines of the Ruby call stack where
|
|
# the drawable was made or given its viewport; otherwise the class name.
|
|
# * At most CAP ids per viewport (the oldest dropped); viewports that
|
|
# vanished without dispose are dropped from the table at PRUNE_AT.
|
|
# * LonaCraft::ViewportGuard.stats "N viewports tracked, M drawables, K
|
|
# disposed by the guard" (80_CraftScene.rb perform_transition traces it).
|
|
# * Switch: Config/LonaCraft.json "viewport_guard" (default true); false
|
|
# means this file defines nothing. Loads after 55_Recipes.rb: before it
|
|
# Config.get answers the defaults without reading the file.
|
|
# * Cost: a few hash operations per drawable made, given a viewport or
|
|
# disposed (plus a short call stack with the trace on); nothing per frame.
|
|
# Keep this file plain ASCII (the game evaluates it as binary text).
|
|
#==============================================================================
|
|
if LonaCraft::Config.get("viewport_guard", true) == true
|
|
|
|
module LonaCraft
|
|
module ViewportGuard
|
|
CAP = 4000 # drawable ids kept per viewport
|
|
PRUNE_AT = 1000 # viewports in the table before vanished ones are dropped
|
|
|
|
@by_vp ||= {} # viewport id => {drawable id => [class name, made at]}
|
|
@vp_of ||= {} # drawable id => viewport id
|
|
@said ||= {} # class name => true once its console line was printed
|
|
@disposed ||= 0 # drawables the guard disposed this run
|
|
|
|
def self.drawable?(obj)
|
|
obj.is_a?(Sprite) || obj.is_a?(Plane) || obj.is_a?(Window) || obj.is_a?(Tilemap)
|
|
end
|
|
|
|
# Forgets drawable +id+ (its viewport found through the table).
|
|
def self.untrack_id(id)
|
|
vid = @vp_of.delete(id)
|
|
return nil if vid.nil?
|
|
t = @by_vp[vid]
|
|
if t
|
|
t.delete(id)
|
|
@by_vp.delete(vid) if t.empty?
|
|
end
|
|
nil
|
|
end
|
|
|
|
def self.untrack(obj)
|
|
untrack_id(obj.__id__)
|
|
rescue StandardError
|
|
nil
|
|
end
|
|
|
|
# +obj+ was made with, or given, viewport +vp+ (nil = none). Called
|
|
# straight from the hooks below: caller(2) starts at the hook's caller.
|
|
def self.track(obj, vp)
|
|
id = obj.__id__
|
|
untrack_id(id)
|
|
return nil unless vp.is_a?(Viewport)
|
|
vid = vp.__id__
|
|
t = @by_vp[vid]
|
|
if t.nil?
|
|
prune if @by_vp.size >= PRUNE_AT
|
|
t = @by_vp[vid] = {}
|
|
end
|
|
if t.size >= CAP
|
|
oldest = t.shift
|
|
@vp_of.delete(oldest[0]) if oldest && @vp_of[oldest[0]] == vid
|
|
end
|
|
name = obj.class.name.to_s
|
|
where = name
|
|
if LonaCraft.trace_on?
|
|
stack = caller(2)
|
|
where = "#{name} at " + stack[0, 4].join(" <- ") if stack && !stack.empty?
|
|
end
|
|
t[id] = [name, where]
|
|
@vp_of[id] = vid
|
|
nil
|
|
rescue StandardError
|
|
nil
|
|
end
|
|
|
|
# Drops the tables of viewports that no longer exist (collected
|
|
# without dispose). Only runs when PRUNE_AT viewports are tracked.
|
|
def self.prune
|
|
@by_vp.keys.each{|vid|
|
|
vp = (ObjectSpace._id2ref(vid) rescue nil)
|
|
# RGSS3's Viewport has no disposed? (self-test 2026-10-07): the guard's own flag
|
|
live = vp.is_a?(Viewport) && !vp.instance_variable_get(:@lonacraft_vpguard_disposed)
|
|
next if live
|
|
t = @by_vp.delete(vid)
|
|
t.each_key{|id| @vp_of.delete(id) if @vp_of[id] == vid } if t
|
|
}
|
|
nil
|
|
rescue StandardError
|
|
nil
|
|
end
|
|
|
|
# Viewport +vp+ is about to be disposed: dispose what is still on it.
|
|
def self.viewport_disposing(vp)
|
|
vid = vp.__id__
|
|
t = @by_vp.delete(vid)
|
|
return nil if t.nil?
|
|
t.each{|id, info|
|
|
@vp_of.delete(id) if @vp_of[id] == vid
|
|
obj = (ObjectSpace._id2ref(id) rescue nil)
|
|
next if obj.nil?
|
|
begin
|
|
next unless drawable?(obj)
|
|
next if obj.disposed?
|
|
next unless obj.viewport.equal?(vp)
|
|
line = "viewport guard: #{obj.class.name} still alive on a disposed Viewport, made at #{info[1]}; disposed it"
|
|
LonaCraft.trace(line)
|
|
unless @said[obj.class.name]
|
|
@said[obj.class.name] = true
|
|
p line
|
|
end
|
|
obj.dispose_lonacraft_vpguard
|
|
@disposed += 1
|
|
rescue StandardError
|
|
end
|
|
}
|
|
nil
|
|
rescue StandardError
|
|
nil
|
|
end
|
|
|
|
def self.stats
|
|
"#{@by_vp.size} viewports tracked, #{@vp_of.size} drawables, #{@disposed} disposed by the guard"
|
|
end
|
|
end
|
|
end
|
|
|
|
# Made with a viewport: Sprite.new(vp), Plane.new(vp), Tilemap.new(vp).
|
|
# (Window.new takes no viewport; it gets one through viewport=.)
|
|
[Sprite, Plane, Tilemap].each{|klass|
|
|
klass.class_eval{
|
|
has_init = private_method_defined?(:initialize) || method_defined?(:initialize)
|
|
done = private_method_defined?(:initialize_lonacraft_vpguard) || method_defined?(:initialize_lonacraft_vpguard)
|
|
if has_init && !done
|
|
alias_method :initialize_lonacraft_vpguard, :initialize
|
|
def initialize(*args)
|
|
initialize_lonacraft_vpguard(*args)
|
|
LonaCraft::ViewportGuard.track(self, args[0])
|
|
end
|
|
end
|
|
}
|
|
}
|
|
|
|
# Given a viewport, and disposed.
|
|
[Sprite, Plane, Window, Tilemap].each{|klass|
|
|
klass.class_eval{
|
|
if method_defined?(:viewport=) && !method_defined?(:viewport_set_lonacraft_vpguard)
|
|
alias_method :viewport_set_lonacraft_vpguard, :viewport=
|
|
def viewport=(vp)
|
|
viewport_set_lonacraft_vpguard(vp)
|
|
LonaCraft::ViewportGuard.track(self, vp)
|
|
vp
|
|
end
|
|
end
|
|
if method_defined?(:dispose) && !method_defined?(:dispose_lonacraft_vpguard)
|
|
alias_method :dispose_lonacraft_vpguard, :dispose
|
|
def dispose
|
|
LonaCraft::ViewportGuard.untrack(self)
|
|
dispose_lonacraft_vpguard
|
|
end
|
|
end
|
|
}
|
|
}
|
|
|
|
class Viewport
|
|
if method_defined?(:dispose) && !method_defined?(:dispose_lonacraft_vpguard)
|
|
alias_method :dispose_lonacraft_vpguard, :dispose
|
|
# RGSS3's Viewport has no disposed? (it raised, the rescue said "disposed" and the
|
|
# guard never ran; self-test 2026-10-07): a flag of our own marks the first dispose.
|
|
def dispose
|
|
unless @lonacraft_vpguard_disposed
|
|
@lonacraft_vpguard_disposed = true
|
|
LonaCraft::ViewportGuard.viewport_disposing(self)
|
|
end
|
|
dispose_lonacraft_vpguard
|
|
end
|
|
end
|
|
end
|
|
|
|
end # viewport_guard switch
|