So I've finally managed to import the C module into python. However, I'm getting a core dump when I try to access the attribute of an object more than once. Here's the code:
node.h:
#pragma once
#include <python3.6m/Python.h>
#include <boost/python.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/detail/api_placeholder.hpp>
using namespace std;
using namespace boost::python;
class Node{
public:
Node(PyObject* attr);
~Node();
PyObject* _attr;
static int number_of_nodes;
int _id;
/*other functions*/
node.cpp:
#include "node.h"
using namespace std;
//using namespace boost::python;
int Node::number_of_nodes = 0;
Node::Node(PyObject* attr){
this->_id = number_of_id;
number_of_nodes++;
Py_INCREF(attr);
this->_attr = attr;
}
Node::~Node(){
}
/*there are some other functions*/
BOOST_PYTHON_MODULE(Node){
class_<Node>("Node", init<PyObject*>())
.def_readwrite("_id", &Node::_id)
.def_readwrite("_attr", &Node::_attr)
.def_readwrite("number_of_nodes", &Node::number_of_nodes)
/*some other functions*/
}
The python file:
from Node import *
class Position:
def __init__(self, i, j ,k):
self._i = i
self._j = j
self._k = k
nodes = []
nb = 1000
for i in range(nb):
nodes.append(Node(Position(i,i,i)))
node = nodes[0]
d = str(node._attr.__dict__)
print(d)
c = str(node._attr)
print(c)
output:
{'_i': 0, '_j': 0, '_k': 0}
Erreur de segmentation (core dumped)
// this means segmentation fault
I tried looking around but it seems that most of the answers have something to do with multithreading which I am not using in this situation. Is it because of Py_INCREF or Py_DECREF?
Thanks