Skip to content

Commit b6cb8ad

Browse files
committed
Improve interface_rc downcasting safety by using Rc::clone and cleaning up pointer handling
1 parent 5f18060 commit b6cb8ad

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

unity-native-plugin-tester/src/interface.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,15 @@ pub unsafe fn get_unity_interface<T: UnityInterfaceBase + UnityInterfaceID + 'st
140140

141141
// Rcの中身をダウンキャストして新しいRcを作成
142142
let any_ref = interface_rc.as_any();
143-
if any_ref. is::<T>() {
144-
// unsafeなポインタ操作を使ってRc<T>を作成
145-
let ptr = Rc::as_ptr(&interface_rc) as *const T;
146-
Rc::from_raw(ptr)
143+
if let Some(concrete_ref) = any_ref.downcast_ref::<T>() {
144+
// Use Rc::clone to safely create an Rc<T>
145+
// First, get a raw pointer from the original Rc
146+
let ptr = Rc::as_ptr(&interface_rc);
147+
// Successfully downcasted, so cast it safely as type T
148+
let concrete_ptr = ptr as *const T;
149+
// Create a new Rc<T> (clone the original Rc to increase the reference count)
150+
std::mem::forget(interface_rc.clone()); // Increase reference count
151+
Rc::from_raw(concrete_ptr)
147152
} else {
148153
panic!("interface is not T");
149154
}
@@ -160,7 +165,5 @@ pub fn initialize_unity_interfaces() {
160165
}
161166

162167
pub fn finalize_unity_interfaces() {
163-
/*unsafe {
164-
UNITY_INTERFACES.res = None;
165-
}*/
168+
UNITY_INTERFACES.get().unwrap().map.lock().unwrap().clear();
166169
}

0 commit comments

Comments
 (0)