Fix crash when creating thread

This commit is contained in:
Haoyu Qiu 2021-09-26 00:02:51 +08:00
parent 3db672c89e
commit 40e188f687
1 changed files with 12 additions and 4 deletions

View File

@ -1768,6 +1768,12 @@ void Thread::_start_func(void *ud) {
Ref<Thread> *tud = (Ref<Thread> *)ud; Ref<Thread> *tud = (Ref<Thread> *)ud;
Ref<Thread> t = *tud; Ref<Thread> t = *tud;
memdelete(tud); memdelete(tud);
Object *target_instance = t->target_callable.get_object();
if (!target_instance) {
ERR_FAIL_MSG(vformat("Could not call function '%s' on previously freed instance to start thread %s.", t->target_callable.get_method(), t->get_id()));
}
Callable::CallError ce; Callable::CallError ce;
const Variant *arg[1] = { &t->userdata }; const Variant *arg[1] = { &t->userdata };
int argc = 0; int argc = 0;
@ -1786,15 +1792,17 @@ void Thread::_start_func(void *ud) {
// We must check if we are in case b). // We must check if we are in case b).
int target_param_count = 0; int target_param_count = 0;
int target_default_arg_count = 0; int target_default_arg_count = 0;
Ref<Script> script = t->target_callable.get_object()->get_script(); Ref<Script> script = target_instance->get_script();
if (script.is_valid()) { if (script.is_valid()) {
MethodInfo mi = script->get_method_info(t->target_callable.get_method()); MethodInfo mi = script->get_method_info(t->target_callable.get_method());
target_param_count = mi.arguments.size(); target_param_count = mi.arguments.size();
target_default_arg_count = mi.default_arguments.size(); target_default_arg_count = mi.default_arguments.size();
} else { } else {
MethodBind *method = ClassDB::get_method(t->target_callable.get_object()->get_class_name(), t->target_callable.get_method()); MethodBind *method = ClassDB::get_method(target_instance->get_class_name(), t->target_callable.get_method());
target_param_count = method->get_argument_count(); if (method) {
target_default_arg_count = method->get_default_argument_count(); target_param_count = method->get_argument_count();
target_default_arg_count = method->get_default_argument_count();
}
} }
if (target_param_count >= 1 && target_default_arg_count < target_param_count) { if (target_param_count >= 1 && target_default_arg_count < target_param_count) {
argc = 1; argc = 1;