Skip to content

Commit

Permalink
remove static sofainit and call unload into each respective module at…
Browse files Browse the repository at this point in the history
… atexit
  • Loading branch information
fredroy committed Aug 8, 2024
1 parent 00e7bd8 commit 637c16a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
11 changes: 11 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ using sofa::helper::logging::Message;
#include <SofaPython3/Sofa/Core/Data/Binding_DataVectorString.h>
#include <SofaPython3/Sofa/Core/Data/Binding_DataContainer.h>

#include <sofa/core/init.h>

namespace sofapython3
{

Expand Down Expand Up @@ -130,6 +132,15 @@ PYBIND11_MODULE(Core, core)
moduleAddBaseMeshTopology(core);
moduleAddPointSetTopologyModifier(core);
moduleAddTaskScheduler(core);

// called when the module is unloaded
auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {

sofa::core::cleanup();

msg_info("SofaPython3.Core") << "Sofa.Core unload()";
}));
}

} ///namespace sofapython3
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Contact information: [email protected] *
******************************************************************************/

#include <sofa/core/init.h>
#include <sofa/helper/init.h>
#include <sofa/helper/logging/Messaging.h>
#include <SofaPython3/PythonEnvironment.h>
Expand Down Expand Up @@ -108,7 +107,6 @@ static void parse_emitter_message_then(py::args args, const Action& action) {
PYBIND11_MODULE(Helper, helper)
{
// These are needed to force the dynamic loading of module dependencies (found in CMakeLists.txt)
sofa::core::init();
sofa::helper::init();

helper.doc() = R"doc(
Expand Down Expand Up @@ -155,6 +153,12 @@ PYBIND11_MODULE(Helper, helper)
moduleAddMessageHandler(helper);
moduleAddVector(helper);
moduleAddSystem(helper);

auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {
sofa::helper::cleanup();
msg_info("SofaPython3.Helper") << "Sofa.Helper unload()";
}));
}

} ///namespace sofapython3
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using sofa::simulation::Simulation;

#include <sofa/core/init.h>
#include <sofa/simulation/init.h>
#include <sofa/simulation/common/init.h>
#include <sofa/simulation/graph/init.h>

namespace py = pybind11;
Expand Down Expand Up @@ -83,6 +84,17 @@ PYBIND11_MODULE(Simulation, simulation)
{
sofa::simulation::node::initTextures(n);
});

// called when the module is unloaded
auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {

sofa::simulation::core::cleanup();
sofa::simulation::common::cleanup();
sofa::simulation::graph::cleanup();

msg_info("SofaPython3.Simulation") << "Sofa.Simulation unload()";
}));
}

} /// namespace sofapython3
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,6 @@ using sofa::helper::logging::MainConsoleMessageHandler;
namespace sofapython3
{


class SofaInitializer
{
public:
// TODO, ces trucs sont fort laid. Normalement ce devrait être une joli plugin qui
// appelle le init.
SofaInitializer(){
sofa::simulation::common::init();
sofa::simulation::graph::init();
}

~SofaInitializer(){
sofa::simulation::common::cleanup();
sofa::simulation::graph::cleanup();
}
};

static std::vector<std::string> getCategories(const std::string& className)
{
std::vector<std::string> categories;
Expand All @@ -109,8 +92,6 @@ static std::vector<std::string> getCategories(const std::string& className)
return categories ;
}

static SofaInitializer s;

/// The first parameter must be named the same as the module file to load.
PYBIND11_MODULE(SofaRuntime, m) {

Expand All @@ -132,13 +113,6 @@ PYBIND11_MODULE(SofaRuntime, m) {
)doc";

// These are needed to force the dynamic loading of module dependencies (found in CMakeLists.txt)
sofa::core::init();
sofa::helper::init();
sofa::simulation::core::init();
sofa::simulation::graph::init();
sofa::simulation::common::init();

// Add the plugin directory to PluginRepository
const std::string& pluginDir = Utils::getExecutableDirectory();
PluginRepository.addFirstPath(pluginDir);
Expand Down Expand Up @@ -192,6 +166,7 @@ PYBIND11_MODULE(SofaRuntime, m) {
m.def("getCategories", &getCategories);

addSubmoduleTimer(m);

}

} // namespace sofapython3

0 comments on commit 637c16a

Please sign in to comment.