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

Memory issues when adding a new function with pybind11 #209

Open
jerenner opened this issue Sep 3, 2021 · 1 comment
Open

Memory issues when adding a new function with pybind11 #209

jerenner opened this issue Sep 3, 2021 · 1 comment

Comments

@jerenner
Copy link

jerenner commented Sep 3, 2021

In attempting to add a new function, callable from Python, similar to electron_count, the function gives a segmentation fault on returning an ElectronCountedData object if no py::array_t objects are included as parameters to that function. However, if one or more py::array_t arrays are given as parameters, it seems to work without problems.

The addition of the function follows the general methodology used in: https://github.com/OpenChemistry/stempy/blob/master/python/image.cpp

A simple example is given here: https://github.com/jerenner/stempy/tree/example
(diff is here: jerenner@60ae390)

The example runs the following code:

{
  Dimensions2D scanDimensions = { 0, 0 };
  Dimensions2D frameSize = { 0, 0 };

  Events events;
  events.resize(10);

  ElectronCountedData ret;
  ret.data = events;
  ret.scanDimensions = scanDimensions;
  ret.frameDimensions = frameSize;

  return ret;
}

using 2 different methods, one which takes a py::array_t and the other that does not.

  1. ElectronCountedData testMethodBasic(int test)
  2. ElectronCountedData testMethodArray(int test, const float test_arr[])

A Jupyter notebook showing the functions run in Python is here: https://github.com/jerenner/4dstem/blob/main/pybind_test.ipynb

For some reason (which could be machine-dependent, as this has only been tested on one machine), the method that takes the array as a parameter does not produce any problems, but the one without any array parameters gives a segmentation fault, which appears to occur on line 28 of https://github.com/OpenChemistry/stempy/blob/master/python/image.cpp:

return py::array(ptr->size(), ptr->data(), capsule);
@cjh1
Copy link
Member

cjh1 commented Sep 10, 2021

@jerenner Thanks for reporting this, we will take a look.

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