From 9cc2988ec7e935b39853e1cc33b4977da4a69825 Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Fri, 2 Aug 2019 19:08:53 -0700 Subject: [PATCH] Fix technical undefined behavior I believe that the current drop implementation has technical undefined behavior, because holds a reference to the `IUnknown` object while that object is in the process of being deallocated. This patch retrieves the function pointer, drops the reference, then does the release. --- src/com.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/com.rs b/src/com.rs index d94d3d3..15aa814 100644 --- a/src/com.rs +++ b/src/com.rs @@ -69,7 +69,11 @@ impl Debug for ComPtr { } impl Drop for ComPtr { fn drop(&mut self) { - unsafe { self.as_unknown().Release(); } + unsafe { + let ptr = self.as_raw() as *mut IUnknown; + let release_fn = (*(*ptr).lpVtbl).Release; + release_fn(ptr); + } } } impl PartialEq> for ComPtr where T: Interface {