EDITED:
I am trying to bind the function that is allocating
#include <iostream>
#include <vector>
#include "pybind11/numpy.h"
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
struct Dummy {
Dummy() : data1(1), data2(2) {};
int data1;
int data2;
};
struct DummyProcessor {
DummyProcessor() {};
int processData()
{
Dummy tmpArr[700000];
// no SEGFAULT if this number is lower
// or if DummyProcessor instance is created directly in C++
return 0;
}
};
PYBIND11_MODULE(test_binding, m) {
m.doc() = "pybind11 example plugin";
pybind11::class_<Dummy>(m, "Dummy")
.def(pybind11::init<>())
;
pybind11::class_<DummyProcessor>(m, "DummyProcessor")
.def(pybind11::init<>())
.def("processData", [](DummyProcessor& self)
{
int status = self.processData();
return status;
})
;
}
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.14)
project(test)
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
)
FetchContent_MakeAvailable(pybind11)
pybind11_add_module(test_binding test.cpp)
target_compile_options(test_binding BEFORE PRIVATE -D_hypot=hypot)
Compiling (add your compiler path):
cmake -G "MinGW Makefiles" -DCMAKE_CXX_COMPILER=PATH_TO_MINGW/g++.exe -DCMAKE_BUILD_TYPE=Debug
PATH_TO_MINGW\mingw32-make.exe Makefile test_binding
Trying to call binding in Python:
import test_binding as t
proc = t.DummyProcessor()
proc.processData() # SEGFAULT