Skip to content

Commit 02c3ee9

Browse files
bakpaulfredroy
authored andcommitted
Add cleanup to core and ensure that the cleanup happens in runSofa
1 parent a6804bd commit 02c3ee9

5 files changed

Lines changed: 19 additions & 1 deletion

File tree

Plugin/src/SofaPython3/DataHelper.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,17 @@ py::slice toSlice(const py::object& o)
249249

250250
std::map<void*, std::pair<int, py::array>>& getObjectCache()
251251
{
252-
static std::map<void*, std::pair<int, py::array>> s_objectcache {} ;
252+
static std::map<void*, std::pair<int, py::array>> s_objectcache{};
253253
return s_objectcache;
254254
}
255255

256+
257+
void clearCache()
258+
{
259+
msg_info("SofaPython3") << "Clearing Sofa.Core cache...";
260+
getObjectCache().clear();
261+
}
262+
256263
void trimCache()
257264
{
258265
auto& memcache = getObjectCache();

Plugin/src/SofaPython3/DataHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ SOFAPYTHON3_API std::string getPathTo(Base* b);
194194
SOFAPYTHON3_API const char* getFormat(const AbstractTypeInfo& nfo);
195195

196196
SOFAPYTHON3_API std::map<void*, std::pair<int, pybind11::array>>& getObjectCache();
197+
SOFAPYTHON3_API void clearCache();
197198
SOFAPYTHON3_API void trimCache();
198199

199200
SOFAPYTHON3_API bool hasArrayFor(BaseData* d);

Plugin/src/SofaPython3/PythonEnvironment.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ void PythonEnvironment::addPluginManagerCallback()
480480
}
481481
}
482482
);
483+
PluginManager::getInstance().addOnPluginCleanupCallbacks(pluginLibraryPath,
484+
[]()
485+
{
486+
PythonEnvironment::Release();
487+
}
488+
);
483489
}
484490

485491
void PythonEnvironment::removePluginManagerCallback()

Plugin/src/SofaPython3/PythonEnvironment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <vector>
2424
#include <string>
2525
#include <sofa/helper/logging/FileInfo.h>
26+
#include <SofaPython3/DataHelper.h>
2627

2728
/// Fixes compile errors:
2829
/// removing all slots macros is necessary if embedded in a Qt project

bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,13 @@ PYBIND11_MODULE(Core, core)
137137
auto atexit = py::module_::import("atexit");
138138
atexit.attr("register")(py::cpp_function([]() {
139139

140+
clearCache();
141+
140142
sofa::core::cleanup();
141143

142144
msg_info("SofaPython3.Core") << "Sofa.Core unload()";
143145
}));
146+
144147
}
145148

146149
} ///namespace sofapython3

0 commit comments

Comments
 (0)