matplotlib-cpp
matplotlib-cpp copied to clipboard
Segmentation fault unless WITHOUT_NUMPY is defined
I use this library in multiple places in my project. It seems that after a while, i.e. multiple includes in different cpp files, you get a segmentation fault. In my case, this is when using plt::scatter, though i doubt the bug is specific to that function. Note that this only manifests itself when compiling with -O3. When I define WITHOUT_NUMPY in my project, the bug disappears, even with -O3. So there is something about the numpy import when compiling with -O3 that only manifests itself when using the library multiple times in different cpp files. Maybe -O3 is compiling out something crucial in the numpy import process. Maybe there is a variable not being used or being ignored, e.g. the return value of some important numpy import function, There is a lot of guess work going on here. Has anybody else seen this?
Yes, although not at all related to optimization level in my case. It segfaults for me in imshow(), I reduced the problem to being in PyArray_SimpleNewFromData function.
I think there is a problem with many of the static variables in the library. Each compilation unit makes a new instance, which means the singletons don`t work at all.
numpy, cpython etc... is initialized multiple times, leading to crashes.
As soon as more than 1 compilation unit includes the header, everything starts to crash.
Oh interesting observation. That seems like an easy fix though
Already spent some ~30 minutes on it, but then noticed, that there are more than 1 singletons with the exact same issue. I will probably try to fix the rest of the issues sometimes next week, if I have time.
I believe I am now having the same issue. As soon as I compiled with multiple cpp files including the header, I start to get segfault related to the details::get_array function.
B.t.w. I stopped investigating the issue, I figured just not using the library and doing it by hand is way faster and easier.