Skip to content

Commit 3126f12

Browse files
Merge #173
173: Use instance_id_or_none in DynamicRefCount to fix #172 r=Bromeon a=WinstonHartnett Closes #172 Co-authored-by: Winston Hartnett <[email protected]>
2 parents 34b00c9 + 2c6b3d0 commit 3126f12

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

godot-core/src/obj/traits.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,21 +264,33 @@ pub mod mem {
264264
impl Memory for DynamicRefCount {
265265
fn maybe_init_ref<T: GodotClass>(obj: &Gd<T>) {
266266
out!(" Dyn::init <{}>", std::any::type_name::<T>());
267-
if obj.instance_id().is_ref_counted() {
268-
StaticRefCount::maybe_init_ref(obj);
267+
if obj
268+
.instance_id_or_none()
269+
.map(|id| id.is_ref_counted())
270+
.unwrap_or(false)
271+
{
272+
StaticRefCount::maybe_init_ref(obj)
269273
}
270274
}
271275

272276
fn maybe_inc_ref<T: GodotClass>(obj: &Gd<T>) {
273277
out!(" Dyn::inc <{}>", std::any::type_name::<T>());
274-
if obj.instance_id().is_ref_counted() {
275-
StaticRefCount::maybe_inc_ref(obj);
278+
if obj
279+
.instance_id_or_none()
280+
.map(|id| id.is_ref_counted())
281+
.unwrap_or(false)
282+
{
283+
StaticRefCount::maybe_inc_ref(obj)
276284
}
277285
}
278286

279287
fn maybe_dec_ref<T: GodotClass>(obj: &Gd<T>) -> bool {
280288
out!(" Dyn::dec <{}>", std::any::type_name::<T>());
281-
if obj.instance_id().is_ref_counted() {
289+
if obj
290+
.instance_id_or_none()
291+
.map(|id| id.is_ref_counted())
292+
.unwrap_or(false)
293+
{
282294
StaticRefCount::maybe_dec_ref(obj)
283295
} else {
284296
false

itest/rust/src/object_test.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ fn object_engine_manual_free() {
531531
} // drop(node)
532532
}
533533

534+
/// Tests the [`DynamicRefCount`] destructor when the underlying [`Object`] is already freed.
535+
#[itest]
536+
fn object_engine_shared_free() {
537+
{
538+
let node = Node::new_alloc();
539+
let _object = node.share().upcast::<Object>();
540+
node.free();
541+
} // drop(_object)
542+
}
543+
534544
#[itest]
535545
fn object_engine_manual_double_free() {
536546
expect_panic("double free()", || {

0 commit comments

Comments
 (0)