Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ error in loadModule #2

Open
rgiot opened this issue Dec 23, 2016 · 2 comments
Open

C++ error in loadModule #2

rgiot opened this issue Dec 23, 2016 · 2 comments

Comments

@rgiot
Copy link

rgiot commented Dec 23, 2016

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

@lava
Copy link
Owner

lava commented Dec 25, 2016

Nice find, thanks! If you care about having your name on the commit, feel free to create a pull request (or a full .patch file using git format-patch), otherwise I'll just apply the fix myself.

@rgiot
Copy link
Author

rgiot commented Dec 27, 2016

No worries, you can patch by yourself ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants