From 01e584bdecf62f89fe541d89cbfe2c5cea8c6bab Mon Sep 17 00:00:00 2001 From: Lauro Oyen Date: Sat, 11 May 2024 11:54:21 +0200 Subject: [PATCH] Fix memory leak caused by wrongly placed addRef() Close #4 --- src/lib.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e8f1124..9eda03f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -117,13 +117,7 @@ impl GlobalSession { pub fn create_session(&self, desc: &SessionDesc) -> Option { let mut session = null_mut(); let res = vcall!(self, createSession(desc, &mut session)); - let session = Session(IUnknown(std::ptr::NonNull::new(session as *mut _)?)); - - // TODO: Without adding an extra reference, the code crashes when Session is dropped. - // Investigate why this is happening, the current solution could cause a memory leak. - unsafe { (session.as_unknown().vtable().ISlangUnknown_addRef)(session.as_raw()) }; - - Some(session) + Some(Session(IUnknown(std::ptr::NonNull::new(session as *mut _)?))) } pub fn find_profile(&self, name: &str) -> ProfileID { @@ -157,7 +151,9 @@ impl Session { let blob = Blob(IUnknown(std::ptr::NonNull::new(diagnostics as *mut _).unwrap())); Err(std::str::from_utf8(blob.as_slice()).unwrap().to_string()) } else { - Ok(Module(IUnknown(std::ptr::NonNull::new(module as *mut _).unwrap()))) + let module = Module(IUnknown(std::ptr::NonNull::new(module as *mut _).unwrap())); + unsafe { (module.as_unknown().vtable().ISlangUnknown_addRef)(module.as_raw()) }; + Ok(module) } }