Skip to content

Commit

Permalink
Free allocated memory using the appropriate API (free/CoTaskMemFree)
Browse files Browse the repository at this point in the history
On Windows the right API needs to be used to free this pointer in the
same way as it was allocated by DXC (using CoTaskMemAlloc). On
non-Windows platforms this call is [delegated] to `free` from libc.

[delegated]: https://github.com/microsoft/DirectXShaderCompiler/blob/a8d9780046cb64a1cea842fa6fc28a250e3e2c09/include/dxc/Support/WinAdapter.h#L46
  • Loading branch information
MarijnS95 committed Nov 10, 2020
1 parent 68ed29b commit c2098e8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ widestring = "0.4.2"
thiserror = "1.0"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["wtypes", "oleauto"] }
winapi = { version = "0.3.9", features = ["wtypes", "oleauto", "combaseapi"] }

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[dev-dependencies]
rspirv = "0.6.0"
48 changes: 27 additions & 21 deletions src/intellisense/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::intellisense::ffi::*;
use crate::os::{BSTR, HRESULT, LPSTR};
use crate::os::{CoTaskMemFree, BSTR, HRESULT, LPSTR};
use crate::utils::HassleError;
use crate::wrapper::Dxc;
use com_rs::ComPtr;
Expand Down Expand Up @@ -178,16 +178,19 @@ impl DxcCursor {
check_hr!(
self.inner
.get_children(skip, max_count, &mut result_length, &mut result),
// get_children allocates a buffer to pass the result in. Vec will
// free it on Drop.
Vec::from_raw_parts(result, result_length as usize, result_length as usize)
.into_iter()
.map(|ptr| {
let mut childcursor = ComPtr::<IDxcCursor>::new();
*childcursor.as_mut_ptr() = ptr;
DxcCursor::new(childcursor)
})
.collect::<Vec<_>>()
{
// get_children allocates a buffer to pass the result in.
let child_cursors = std::slice::from_raw_parts(result, result_length as usize)
.iter()
.map(|&ptr| {
let mut childcursor = ComPtr::<IDxcCursor>::new();
*childcursor.as_mut_ptr() = ptr;
DxcCursor::new(childcursor)
})
.collect::<Vec<_>>();
CoTaskMemFree(result as *mut _);
child_cursors
}
)
}
}
Expand Down Expand Up @@ -359,16 +362,19 @@ impl DxcCursor {
&mut result_length,
&mut result
),
// find_references_in_file allocates a buffer to pass the result in.
// Vec will free it on Drop.
Vec::from_raw_parts(result, result_length as usize, result_length as usize)
.into_iter()
.map(|ptr| {
let mut childcursor = ComPtr::<IDxcCursor>::new();
*childcursor.as_mut_ptr() = ptr;
DxcCursor::new(childcursor)
})
.collect::<Vec<_>>()
{
// find_references_in_file allocates a buffer to pass the result in.
let child_cursors = std::slice::from_raw_parts(result, result_length as usize)
.iter()
.map(|&ptr| {
let mut childcursor = ComPtr::<IDxcCursor>::new();
*childcursor.as_mut_ptr() = ptr;
DxcCursor::new(childcursor)
})
.collect::<Vec<_>>();
CoTaskMemFree(result as *mut _);
child_cursors
}
)
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod os_defs {
ntdef::{HRESULT, LPCSTR, LPCWSTR, LPSTR, LPWSTR, WCHAR},
wtypes::BSTR,
};

pub use winapi::um::combaseapi::CoTaskMemFree;
}

#[cfg(not(windows))]
Expand All @@ -18,6 +20,11 @@ mod os_defs {
pub type BSTR = *mut OLECHAR;
pub type LPBSTR = *mut BSTR;
pub type HRESULT = i32;

#[allow(non_snake_case)]
pub unsafe fn CoTaskMemFree(p: *mut libc::c_void) {
libc::free(p)
}
}

pub use os_defs::*;

0 comments on commit c2098e8

Please sign in to comment.