Skip to content

Commit 4d94f4f

Browse files
bors[bot]Waridley
andauthored
Merge #925
925: fix leaks in as_arg tests r=Bromeon a=Waridley Multiple objects were being leaked by the `test_as_arg` tests. Dropping the parent in the `add_[node/instance]_with` methods solved most of them, and replacing the dummy node with a `MaybeUninit` for the shared ref tests fixed the rest. (I also edited the message in `test_array_debug` to make it clearer to me what's supposed to happen. Co-authored-by: Waridley <[email protected]>
2 parents 1b4754c + c363fc0 commit 4d94f4f

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

gdnative-core/src/core_types/variant_array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,8 @@ godot_test!(
614614
test_array_debug {
615615
use std::panic::catch_unwind;
616616

617-
println!(" -- expected 4 'Index 3 out of bounds (len 3)' error messages for edge cases");
618-
println!(" -- the test is successful when and only when these errors are shown");
617+
println!(" -- expecting four 'Index 3 out of bounds (len 3)' panic messages for edge cases");
618+
println!(" -- the test is successful when and only when these four errors are shown");
619619

620620
let arr = VariantArray::new(); // []
621621
arr.push(&Variant::new("hello world"));

test/src/test_as_arg.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use gdnative::derive::{methods, NativeClass};
22
use gdnative::prelude::NodeResolveExt;
33
use gdnative::prelude::*;
4+
use std::mem::MaybeUninit;
45
use std::ops::Deref;
56

67
pub(crate) fn register(handle: InitHandle) {
@@ -38,10 +39,10 @@ crate::godot_itest! { test_as_arg_ref {
3839
add_node_with(|n: Ref<Node2D, Unique>| n.into_shared());
3940

4041
// &Ref<T, Shared>
41-
let mut keeper = Node2D::new().into_shared(); // keep Ref<T, Shared> alive so we can return a reference to it
42+
let mut keeper: MaybeUninit<Ref<Node2D, Shared>> = MaybeUninit::uninit(); // keep Ref<T, Shared> alive so we can return a reference to it
4243
add_node_with(|n: Ref<Node2D, Unique>| {
43-
keeper = n.into_shared();
44-
&keeper
44+
keeper.write(n.into_shared());
45+
unsafe { keeper.assume_init_ref() }
4546
});
4647

4748
// TRef<T, Shared>
@@ -56,10 +57,10 @@ crate::godot_itest! { test_as_arg_instance {
5657
add_instance_with(|n: Instance<MyNode, Unique>| n.into_shared());
5758

5859
// &Instance<T, Shared>
59-
let mut keeper = MyNode { secret: "" }.emplace().into_shared(); // keep Instance<T, Shared> alive so we can return a reference to it
60+
let mut keeper: MaybeUninit<Instance<MyNode, Shared>> = MaybeUninit::uninit(); // keep Instance<T, Shared> alive so we can return a reference to it
6061
add_instance_with(|n: Instance<MyNode, Unique>| {
61-
keeper = n.into_shared();
62-
&keeper
62+
keeper.write(n.into_shared());
63+
unsafe { keeper.assume_init_ref() }
6364
});
6465

6566
// TInstance<T, Shared>
@@ -84,6 +85,7 @@ where
8485
let found_tref = unsafe { found.assume_safe() };
8586

8687
assert_eq!(found_tref.get_instance_id(), child_id);
88+
parent.free()
8789
}
8890

8991
fn add_instance_with<F, T>(to_arg: F)
@@ -117,5 +119,6 @@ where
117119
assert_eq!(node.get_instance_id(), child_id);
118120
assert_eq!(user.secret, "yes");
119121
})
120-
.expect("found.map()")
122+
.expect("found.map()");
123+
parent.free()
121124
}

0 commit comments

Comments
 (0)