diff --git a/binding/binding-util.cpp b/binding/binding-util.cpp index 7e5d1b73..9349f197 100644 --- a/binding/binding-util.cpp +++ b/binding/binding-util.cpp @@ -70,16 +70,14 @@ const RbException excToRbExc[] = { MKXP /* MKXPError */ }; -VALUE excToRbClass(const Exception &exc) { +void raiseRbExc(Exception *exc) { + VALUE str = rb_str_new2(exc->msg.c_str()); RbData *data = getRbData(); - return data->exc[excToRbExc[exc.type]]; -} + VALUE excClass = data->exc[excToRbExc[exc->type]]; -void raiseRbExc(Exception exc) { - RbData *data = getRbData(); - VALUE excClass = data->exc[excToRbExc[exc.type]]; + delete exc; - rb_raise(excClass, "%s", exc.msg); + rb_exc_raise(rb_class_new_instance(1, &str, excClass)); } void raiseDisposedAccess(VALUE self) { @@ -311,9 +309,7 @@ int rb_get_args(int argc, VALUE *argv, const char *format, ...) { /* Raising here is probably fine, right? * If any methods allocate something with a destructor before * calling this then they can probably be fixed to not do that. */ - Exception e(*exc); - delete exc; - rb_raise(excToRbClass(e), "%s", e.msg); + raiseRbExc(exc); } return 0; @@ -333,7 +329,7 @@ static void *gvl_guard(void *args) { try{ return gvl_args->func(gvl_args->args); } catch (const Exception &e) { - gvl_args->exc = new Exception(e.type, e.msg); + gvl_args->exc = new Exception(e); } return 0; } @@ -346,7 +342,7 @@ void *drop_gvl_guard(void *(*func)(void *), void *args, Exception *&exc = gvl_args.exc; if (exc){ - Exception e(exc->type, exc->msg); + Exception e(*exc); delete exc; throw e; } diff --git a/binding/binding-util.h b/binding/binding-util.h index 4839e987..1cb6bc2f 100644 --- a/binding/binding-util.h +++ b/binding/binding-util.h @@ -75,8 +75,7 @@ RbData *getRbData(); struct Exception; -VALUE excToRbClass(const Exception &exc); -void raiseRbExc(Exception exc); +void raiseRbExc(Exception *exc); #if RAPI_MAJOR >= 2 void *drop_gvl_guard(void *(*func)(void *), void *args, @@ -494,9 +493,7 @@ static inline VALUE rb_file_open_str(VALUE filename, const char *mode) { exc = new Exception(e); \ } \ if (exc) { \ - Exception e(*exc); \ - delete exc; \ - rb_raise(excToRbClass(e), "%s", e.msg); \ + raiseRbExc(exc); \ } \ return Qnil; \ } diff --git a/binding/serializable-binding.h b/binding/serializable-binding.h index 4536503b..d6f42bea 100644 --- a/binding/serializable-binding.h +++ b/binding/serializable-binding.h @@ -44,10 +44,8 @@ serializableDump(int, VALUE *, VALUE self) } if (exc) { - Exception e(exc->type, exc->msg); - delete exc; - rb_raise(excToRbClass(e), "%s", e.msg); - } + raiseRbExc(exc); + } return data; } diff --git a/src/util/exception.h b/src/util/exception.h index 3f52fca7..ae956000 100644 --- a/src/util/exception.h +++ b/src/util/exception.h @@ -48,7 +48,7 @@ struct Exception }; Type type; - char msg[512]; + std::string msg; Exception(Type type, const char *format, ...) : type(type) @@ -56,7 +56,8 @@ struct Exception va_list ap; va_start(ap, format); - vsnprintf(msg, sizeof(msg), format, ap); + msg.resize(512); + vsnprintf(&msg[0], msg.size(), format, ap); va_end(ap); }