Tilemap/VX: Ensure proxy objects don't outlive their parents

Either of these would previously crash (same for VX):

tm = Tilemap.new
at = tm.autotiles
tm = nil
GC.start
at[0] = Bitmap.new(1, 1)

tm = Tilemap.new
at = tm.autotiles
tm.dispose
at[0] = Bitmap.new(1, 1)

Funnily, this makes RMXP itself crash too, but crashing is
never acceptable except for possibly resource exhaustion.
This commit is contained in:
Jonas Kulla 2014-09-26 18:21:50 +02:00
parent e9d0d0566b
commit 9758e660c4
9 changed files with 41 additions and 12 deletions

View file

@ -41,6 +41,7 @@ struct
SYMD(tone),
SYMD(rect),
SYMD(src_rect),
SYMD(tilemap),
SYMD(tileset),
SYMD(autotiles),
SYMD(map_data),

View file

@ -42,6 +42,7 @@ enum CommonSymbol
CStone,
CSrect,
CSsrc_rect,
CStilemap,
CStileset,
CSautotiles,
CSmap_data,

View file

@ -93,13 +93,18 @@ MRB_METHOD(tilemapInitialize)
wrapProperty(mrb, self, &t->getAutotiles(), CSautotiles, TilemapAutotilesType);
mrb_value autotilesObj = mrb_iv_get(mrb, self, getMrbData(mrb)->symbols[CSautotiles]);
MrbData &mrbData = *getMrbData(mrb);
mrb_value autotilesObj = mrb_iv_get(mrb, self, mrbData.symbols[CSautotiles]);
mrb_value ary = mrb_ary_new_capa(mrb, 7);
for (int i = 0; i < 7; ++i)
mrb_ary_push(mrb, ary, mrb_nil_value());
mrb_iv_set(mrb, autotilesObj, getMrbData(mrb)->symbols[CSarray], ary);
mrb_iv_set(mrb, autotilesObj, mrbData.symbols[CSarray], ary);
/* Circular reference so both objects are always
* alive at the same time */
mrb_iv_set(mrb, autotilesObj, mrbData.symbols[CStilemap], self);
return self;
}