fix memory leak in PyList::set_item and insert_item

This commit is contained in:
Nikolay Kim 2017-07-19 22:22:19 -07:00
parent 1035aaae49
commit e23c7247e6
3 changed files with 9 additions and 12 deletions

View File

@ -97,11 +97,6 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
unsafe { _pyo3::PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}
impl std::convert::AsRef<PyObjectRef> for #cls {
fn as_ref(&self) -> &_pyo3::PyObjectRef {
unsafe{&*(self.as_ptr() as *const _pyo3::PyObjectRef)}
}
}
impl<'a> std::convert::From<&'a mut #cls> for &'a #cls
{
fn from(ob: &'a mut #cls) -> Self {

View File

@ -216,7 +216,7 @@ impl AsPyRef<PyObjectRef> for PyObject {
#[inline]
fn as_ref(&self, _py: Python) -> &PyObjectRef {
unsafe {std::mem::transmute(self)}
unsafe {&*(self as *const _ as *mut PyObjectRef)}
}
#[inline]
fn as_mut(&self, _py: Python) -> &mut PyObjectRef {

View File

@ -71,10 +71,11 @@ impl PyList {
pub fn set_item<I>(&self, index: isize, item: I) -> PyResult<()>
where I: ToPyObject
{
item.with_borrowed_ptr(self.py(), |item| unsafe {
let item = item.to_object(self.py());
unsafe {
err::error_on_minusone(
self.py(), ffi::PyList_SetItem(self.as_ptr(), index, item))
})
self.py(), ffi::PyList_SetItem(self.as_ptr(), index, item.into_ptr()))
}
}
/// Inserts an item at the specified index.
@ -83,10 +84,11 @@ impl PyList {
pub fn insert_item<I>(&self, index: isize, item: I) -> PyResult<()>
where I: ToPyObject
{
item.with_borrowed_ptr(self.py(), |item| unsafe {
let item = item.to_object(self.py());
unsafe {
err::error_on_minusone(
self.py(), ffi::PyList_Insert(self.as_ptr(), index, item))
})
self.py(), ffi::PyList_Insert(self.as_ptr(), index, item.into_ptr()))
}
}
#[inline]