Fix memory leak caused by wrongly placed addRef()

Close #4
This commit is contained in:
Lauro Oyen 2024-05-11 11:54:21 +02:00
parent 095c719ff8
commit 01e584bdec

View File

@ -117,13 +117,7 @@ impl GlobalSession {
pub fn create_session(&self, desc: &SessionDesc) -> Option<Session> { pub fn create_session(&self, desc: &SessionDesc) -> Option<Session> {
let mut session = null_mut(); let mut session = null_mut();
let res = vcall!(self, createSession(desc, &mut session)); let res = vcall!(self, createSession(desc, &mut session));
let session = Session(IUnknown(std::ptr::NonNull::new(session as *mut _)?)); Some(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)
} }
pub fn find_profile(&self, name: &str) -> ProfileID { 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())); let blob = Blob(IUnknown(std::ptr::NonNull::new(diagnostics as *mut _).unwrap()));
Err(std::str::from_utf8(blob.as_slice()).unwrap().to_string()) Err(std::str::from_utf8(blob.as_slice()).unwrap().to_string())
} else { } 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)
} }
} }