diff --git a/src/NativeBridge/ManagedInterop.cpp b/src/NativeBridge/ManagedInterop.cpp index 074492d8..497d5d0f 100644 --- a/src/NativeBridge/ManagedInterop.cpp +++ b/src/NativeBridge/ManagedInterop.cpp @@ -6,16 +6,22 @@ #include "DataViewInterop.h" #include "ManagedInterop.h" +inline void destroyManagerCObject(PyObject* obj) { + auto* b = static_cast(PyCapsule_GetPointer(obj, NULL)); + if (b) { delete b; } +} + #define SetDict2(cpptype, nptype); \ {\ PythonObject* col = dynamic_cast*>(column);\ auto shrd = col->GetData();\ auto* data = shrd->data();\ + bp::handle<> h(::PyCapsule_New((void*)column, NULL, (PyCapsule_Destructor)&destroyManagerCObject));\ dict[_names[i]] = np::from_data(\ data,\ np::dtype::get_builtin(),\ bp::make_tuple(shrd->size()),\ - bp::make_tuple(sizeof(nptype)), bp::object());\ + bp::make_tuple(sizeof(nptype)), bp::object(h));\ } #define SetDict1(type) SetDict2(type, type) @@ -25,11 +31,12 @@ PythonObject* col = dynamic_cast*>(column);\ auto shrd = col->GetData();\ auto* data = shrd->data();\ + bp::handle<> h(::PyCapsule_New((void*)column, NULL, (PyCapsule_Destructor)&destroyManagerCObject));\ np::ndarray npdata = np::from_data(\ data,\ np::dtype::get_builtin(),\ bp::make_tuple(shrd->size()),\ - bp::make_tuple(sizeof(float)), bp::object());\ + bp::make_tuple(sizeof(float)), bp::object(h));\ if (keyNames == nullptr)\ {\ dict[_names[i]] = npdata;\ @@ -305,6 +312,7 @@ bp::dict EnvironmentBlock::GetData() list.append(obj); } dict[_names[i]] = list; + delete column; } break; case TS: diff --git a/src/NativeBridge/PythonInterop.h b/src/NativeBridge/PythonInterop.h index 7ed17a99..9654476a 100644 --- a/src/NativeBridge/PythonInterop.h +++ b/src/NativeBridge/PythonInterop.h @@ -62,7 +62,7 @@ template class PythonObject : public PythonObjectBase { protected: - std::shared_ptr> _pData; + std::vector* _pData; size_t _numRows; size_t _numCols; @@ -71,7 +71,7 @@ class PythonObject : public PythonObjectBase PythonObject(const int& kind, size_t numRows = 1, size_t numCols = 1); virtual ~PythonObject(); void SetAt(size_t nRow, size_t nCol, const T& value); - const std::shared_ptr >& GetData() const; + const std::vector* GetData() const; }; template @@ -81,7 +81,7 @@ inline PythonObject::PythonObject(const int& kind, size_t numRows, size_t num _numRows = numRows; _numCols = numCols; - _pData = std::make_shared>(); + _pData = new std::vector(); if (_numRows > 0) _pData->reserve(_numRows*_numCols); } @@ -89,6 +89,7 @@ inline PythonObject::PythonObject(const int& kind, size_t numRows, size_t num template inline PythonObject::~PythonObject() { + delete _pData; } template @@ -101,7 +102,7 @@ inline void PythonObject::SetAt(size_t nRow, size_t nCol, const T& value) } template -inline const std::shared_ptr>& PythonObject::GetData() const +inline const std::vector* PythonObject::GetData() const { return _pData; } \ No newline at end of file