Use rb_obj_alloc instead of rb_class_new_instance in binding-sandbox

Somehow, using `rb_class_new_instance` causes use-after-free errors in
the subsequent call to `set_private_data`.

Well, `rb_obj_alloc` is what's used in the normal MRI bindings. I just
replaced the `rb_obj_alloc` calls with `rb_class_new_instance`
at some point in binding-sandbox. Guess that wasn't a good idea.
This commit is contained in:
刘皓 2025-02-16 23:30:09 -05:00
parent 58cf92e482
commit cae4166765
No known key found for this signature in database
GPG key ID: 7901753DB465B711
7 changed files with 15 additions and 15 deletions

View file

@ -193,7 +193,7 @@ namespace mkxp_sandbox {
BOOST_ASIO_CORO_REENTER (this) { BOOST_ASIO_CORO_REENTER (this) {
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, new Rect(get_private_data<Bitmap>(self)->rect())); set_private_data(obj, new Rect(get_private_data<Bitmap>(self)->rect()));
} }
@ -216,7 +216,7 @@ namespace mkxp_sandbox {
SANDBOX_AWAIT_AND_SET(str, rb_string_value_cstr, &text); SANDBOX_AWAIT_AND_SET(str, rb_string_value_cstr, &text);
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, new Rect(get_private_data<Bitmap>(self)->textSize((const char *)(**sb() + str)))); set_private_data(obj, new Rect(get_private_data<Bitmap>(self)->textSize((const char *)(**sb() + str))));
} }

View file

@ -40,7 +40,7 @@ namespace mkxp_sandbox {
BOOST_ASIO_CORO_REENTER (this) { BOOST_ASIO_CORO_REENTER (this) {
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, new Color()); set_private_data(obj, new Color());
SANDBOX_AWAIT(rb_iv_set, self, "color", obj); SANDBOX_AWAIT(rb_iv_set, self, "color", obj);
} }

View file

@ -68,13 +68,13 @@ namespace mkxp_sandbox {
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &plane->getColor()); set_private_data(obj, &plane->getColor());
SANDBOX_AWAIT(rb_iv_set, self, "color", obj); SANDBOX_AWAIT(rb_iv_set, self, "color", obj);
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Tone"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Tone");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &plane->getTone()); set_private_data(obj, &plane->getTone());
SANDBOX_AWAIT(rb_iv_set, self, "tone", obj); SANDBOX_AWAIT(rb_iv_set, self, "tone", obj);

View file

@ -63,19 +63,19 @@ namespace mkxp_sandbox {
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &sprite->getSrcRect()); set_private_data(obj, &sprite->getSrcRect());
SANDBOX_AWAIT(rb_iv_set, self, "src_rect", obj); SANDBOX_AWAIT(rb_iv_set, self, "src_rect", obj);
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &sprite->getColor()); set_private_data(obj, &sprite->getColor());
SANDBOX_AWAIT(rb_iv_set, self, "color", obj); SANDBOX_AWAIT(rb_iv_set, self, "color", obj);
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Tone"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Tone");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &sprite->getTone()); set_private_data(obj, &sprite->getTone());
SANDBOX_AWAIT(rb_iv_set, self, "tone", obj); SANDBOX_AWAIT(rb_iv_set, self, "tone", obj);

View file

@ -144,19 +144,19 @@ namespace mkxp_sandbox {
SANDBOX_AWAIT_AND_SET(id, rb_intern, "TilemapAutotiles"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "TilemapAutotiles");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &tilemap->getAutotiles()); set_private_data(obj, &tilemap->getAutotiles());
SANDBOX_AWAIT(rb_iv_set, self, "autotiles", obj); SANDBOX_AWAIT(rb_iv_set, self, "autotiles", obj);
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &tilemap->getColor()); set_private_data(obj, &tilemap->getColor());
SANDBOX_AWAIT(rb_iv_set, self, "color", obj); SANDBOX_AWAIT(rb_iv_set, self, "color", obj);
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Tone"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Tone");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &tilemap->getTone()); set_private_data(obj, &tilemap->getTone());
SANDBOX_AWAIT(rb_iv_set, self, "tone", obj); SANDBOX_AWAIT(rb_iv_set, self, "tone", obj);

View file

@ -65,19 +65,19 @@ namespace mkxp_sandbox {
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &viewport->getRect()); set_private_data(obj, &viewport->getRect());
SANDBOX_AWAIT(rb_iv_set, self, "rect", obj); SANDBOX_AWAIT(rb_iv_set, self, "rect", obj);
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Color");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &viewport->getColor()); set_private_data(obj, &viewport->getColor());
SANDBOX_AWAIT(rb_iv_set, self, "color", obj); SANDBOX_AWAIT(rb_iv_set, self, "color", obj);
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Tone"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Tone");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(obj, rb_obj_alloc, klass);
set_private_data(obj, &viewport->getTone()); set_private_data(obj, &viewport->getTone());
SANDBOX_AWAIT(rb_iv_set, self, "tone", obj); SANDBOX_AWAIT(rb_iv_set, self, "tone", obj);

View file

@ -61,7 +61,7 @@ namespace mkxp_sandbox {
window->initDynAttribs(); window->initDynAttribs();
SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect"); SANDBOX_AWAIT_AND_SET(id, rb_intern, "Rect");
SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id); SANDBOX_AWAIT_AND_SET(klass, rb_const_get, sb()->rb_cObject(), id);
SANDBOX_AWAIT_AND_SET(cursor_obj, rb_class_new_instance, 0, NULL, klass); SANDBOX_AWAIT_AND_SET(cursor_obj, rb_obj_alloc, klass);
set_private_data(cursor_obj, &window->getCursorRect()); set_private_data(cursor_obj, &window->getCursorRect());
SANDBOX_AWAIT(rb_iv_set, self, "cursor_rect", cursor_obj); SANDBOX_AWAIT(rb_iv_set, self, "cursor_rect", cursor_obj);
GFX_UNLOCK GFX_UNLOCK