Skip to content

Commit 2e14deb

Browse files
authored
Merge pull request #670 from TitanNano/jovan/script_instance_list_free
Correctly free property and method lists
2 parents 9cd97db + b14f4d1 commit 2e14deb

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

godot-core/src/engine/script_instance.rs

+16-31
Original file line numberDiff line numberDiff line change
@@ -568,34 +568,19 @@ mod script_instance_info {
568568
p_instance: sys::GDExtensionScriptInstanceDataPtr,
569569
p_prop_info: *const sys::GDExtensionPropertyInfo,
570570
) {
571-
let ctx = || {
572-
format!(
573-
"error while calling {}::get_property_list",
574-
type_name::<T>()
575-
)
576-
};
577-
578-
let length = handle_panic(ctx, || {
579-
let instance = instance_data_as_script_instance::<T>(p_instance);
580-
581-
borrow_instance(instance).get_property_list().len()
582-
})
583-
.unwrap_or_default();
584-
585-
// SAFETY: p_prop_info is expected to have been created by get_property_list_func
586-
// and therefore should have the same length as before. get_propery_list_func
587-
// also guarantees that both vector length and capacity are equal.
588-
let _drop = transfer_ptr_list_from_godot(p_prop_info, length);
589-
590-
// now drop the backing data
591571
let instance = instance_data_as_script_instance::<T>(p_instance);
592572

593-
let _drop = instance
573+
let vec = instance
594574
.property_list
595575
.lock()
596576
.expect("mutex should not be poisoned")
597577
.remove(&p_prop_info)
598-
.expect("we can not free a list that has not been allocated");
578+
.expect("Godot is trying to free the property list, but none has been set");
579+
580+
// SAFETY: p_prop_info is expected to have been created by get_property_list_func
581+
// and therefore should have the same length as before. get_propery_list_func
582+
// also guarantees that both vector length and capacity are equal.
583+
let _drop = transfer_ptr_list_from_godot(p_prop_info, vec.len());
599584
}
600585

601586
/// # Safety
@@ -706,20 +691,20 @@ mod script_instance_info {
706691
p_instance: sys::GDExtensionScriptInstanceDataPtr,
707692
p_method_info: *const sys::GDExtensionMethodInfo,
708693
) {
709-
let ctx = || format!("error while calling {}::get_method_list", type_name::<T>());
710-
711-
let length = handle_panic(ctx, || {
712-
let instance = instance_data_as_script_instance::<T>(p_instance);
694+
let instance = instance_data_as_script_instance::<T>(p_instance);
713695

714-
borrow_instance(instance).get_method_list().len()
715-
})
716-
.unwrap_or_default();
696+
let vec = instance
697+
.method_list
698+
.lock()
699+
.expect("method_list mutex should not be poisoned")
700+
.remove(&p_method_info)
701+
.expect("Godot is trying to free the method_list, but none has been set");
717702

718703
// SAFETY: p_method_info is expected to have been created by get_method_list_func and therefore should have the same length as before.
719704
// get_method_list_func also guarantees that both vector length and capacity are equal.
720-
let vec = transfer_ptr_list_from_godot(p_method_info, length);
705+
let vec_sys = transfer_ptr_list_from_godot(p_method_info, vec.len());
721706

722-
vec.into_iter().for_each(|method_info| {
707+
vec_sys.into_iter().for_each(|method_info| {
723708
transfer_ptr_list_from_godot(
724709
method_info.arguments,
725710
method_info.argument_count as usize,

0 commit comments

Comments
 (0)