Skip to content

Commit

Permalink
Fix memory leak in PyTypeBuilder::build
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Aug 18, 2023
1 parent 7f32ed9 commit 00d3507
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions newsfragments/3400.fixed.md
@@ -0,0 +1 @@
Fix memory leak in `PyTypeBuilder::build`.
5 changes: 4 additions & 1 deletion src/pyclass/create_type_object.rs
Expand Up @@ -372,8 +372,9 @@ impl PyTypeBuilder {
// Safety: python expects this empty slot
unsafe { self.push_slot(0, ptr::null_mut::<c_void>()) }

let name = py_class_qualified_name(module_name, name)?;
let mut spec = ffi::PyType_Spec {
name: py_class_qualified_name(module_name, name)?,
name,
basicsize: basicsize as c_int,
itemsize: 0,

Expand All @@ -386,6 +387,8 @@ impl PyTypeBuilder {
// Safety: We've correctly setup the PyType_Spec at this point
let type_object: Py<PyType> =
unsafe { Py::from_owned_ptr_or_err(py, ffi::PyType_FromSpec(&mut spec))? };
// Safety: `name` is a valid pointer obtained from `CString::into_raw` in `py_class_qualified_name`
drop(unsafe { CString::from_raw(name) });

for cleanup in std::mem::take(&mut self.cleanup) {
cleanup(&self, type_object.as_ref(py).as_type_ptr());
Expand Down

0 comments on commit 00d3507

Please sign in to comment.