matplotlib-cpp icon indicating copy to clipboard operation
matplotlib-cpp copied to clipboard

Segmentation fault unless WITHOUT_NUMPY is defined

Open pfeatherstone opened this issue 4 years ago • 6 comments

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?

pfeatherstone avatar Apr 15 '20 13:04 pfeatherstone

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.

ruabmbua avatar Dec 21 '21 12:12 ruabmbua

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.

ruabmbua avatar Dec 30 '21 19:12 ruabmbua

Oh interesting observation. That seems like an easy fix though

pfeatherstone avatar Dec 30 '21 20:12 pfeatherstone

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.

ruabmbua avatar Dec 30 '21 21:12 ruabmbua

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.

Jerry-Ma avatar Jul 01 '22 00:07 Jerry-Ma

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.

ruabmbua avatar Aug 13 '22 12:08 ruabmbua