Skip to content

C++ error in loadModule #2

@rgiot

Description

@rgiot

With g++6.2.0, wrappy raise the following error when launching example_plot

terminate called after throwing an instance of 'wrappy::WrappyError'
  what():  Wrappy: Lookup of function matplotlib.pyplot.plotfailed.
zsh: abort (core dumped)  ./example_plot

The following patch solves the issue

diff --git a/wrappy.cpp b/wrappy.cpp
index 9a0ec17..bca9895 100644
--- a/wrappy.cpp
+++ b/wrappy.cpp
@@ -68,9 +68,9 @@ PythonObject loadModule(const std::string& name, size_t& dot)
     PythonObject module;
     while (!module && dot != std::string::npos) {
         dot = name.rfind('.', dot-1);
-        auto prefix = name.substr(0, dot).c_str();
+        auto prefix = name.substr(0, dot);
         module = PythonObject(PythonObject::owning {},
-            PyImport_ImportModule(prefix));
+            PyImport_ImportModule(prefix.c_str()));
     }

     return module;

In the original version, prefix is a char * which address the buffer of a temporary string object which is supposed to not live the next line (and thus when used by PyImport_ImportModule).
In the corrected version, prefix is a string which lives until the end of the function, its buffer is used when the string is still alive

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions