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

Initialize prop_id_ in NPyMechObj. #2604

Merged
merged 7 commits into from
Nov 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/nrnpython/nrnpy_nrn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,21 @@
PyObject_HEAD
NPySegObj* pyseg_;
Prop* prop_;
neuron::container::non_owning_identifier_without_container prop_id_{};
// Following cannot be initialized when NPyMechObj allocated by Python. See new_pymechobj
// wrapper.
neuron::container::non_owning_identifier_without_container prop_id_;
int type_;
} NPyMechObj;

typedef struct {
PyObject_HEAD
NPyMechObj* pymech_{};
NPyMechObj* pymech_;
} NPyMechOfSegIter;

typedef struct {
PyObject_HEAD
NPyMechObj* pymech_{};
NPyDirectMechFunc* f_{};
NPyMechObj* pymech_;
NPyDirectMechFunc* f_;
} NPyMechFunc;

typedef struct {
Expand Down Expand Up @@ -257,12 +259,26 @@
static void NPyMechObj_dealloc(NPyMechObj* self) {
// printf("NPyMechObj_dealloc %p %s\n", self, self->ob_type->tp_name);
Py_XDECREF(self->pyseg_);
// Must manually call destructor since it was manually constructed in new_pymechobj wrapper
self->prop_id_.~non_owning_identifier_without_container();
nrnhines marked this conversation as resolved.
Show resolved Hide resolved
((PyObject*) self)->ob_type->tp_free((PyObject*) self);
}

static NPyMechObj* new_pymechobj() {
NPyMechObj* m = PyObject_New(NPyMechObj, pmech_generic_type);
if (m) {
// Use "placement new" idiom since Python C allocation cannot call the initializer to start
// it as "null". So later `a = b` might segfault because copy constructor decrements the
// refcount of `a`s nonsense memory.
new (&m->prop_id_) neuron::container::non_owning_identifier_without_container;
}

return m;
}

// Only call if p is valid
static NPyMechObj* new_pymechobj(NPySegObj* pyseg, Prop* p) {
NPyMechObj* m = PyObject_New(NPyMechObj, pmech_generic_type);
NPyMechObj* m = new_pymechobj();
if (!m) {
return NULL;
}
Expand Down Expand Up @@ -436,6 +452,7 @@
// printf("NPyMechObj_new %p %s\n", self,
// ((PyObject*)self)->ob_type->tp_name);
if (self != NULL) {
new (self) NPyMechObj;

Check warning on line 455 in src/nrnpython/nrnpy_nrn.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_nrn.cpp#L455

Added line #L455 was not covered by tests
self->pyseg_ = pyseg;
Py_INCREF(self->pyseg_);
}
Expand Down Expand Up @@ -1669,7 +1686,7 @@
if (!r) {
return NULL;
}
r->pymech_ = PyObject_New(NPyMechObj, pmech_generic_type);
r->pymech_ = new_pymechobj();
r->pymech_->pyseg_ = PyObject_New(NPySegObj, psegment_type);
r->pymech_->pyseg_->pysec_ = sec;
Py_INCREF(sec);
Expand Down Expand Up @@ -1916,7 +1933,7 @@
sym = ((NPyRangeVar*) rv)->sym_;
if (ISARRAY(sym)) {
NPyRangeVar* r = PyObject_New(NPyRangeVar, range_type);
r->pymech_ = PyObject_New(NPyMechObj, pmech_generic_type);
r->pymech_ = new_pymechobj();
r->pymech_->pyseg_ = self;
Py_INCREF(r->pymech_->pyseg_);
r->sym_ = sym;
Expand Down Expand Up @@ -1944,7 +1961,7 @@
sym->type == RANGEVAR) {
if (ISARRAY(sym)) {
NPyRangeVar* r = PyObject_New(NPyRangeVar, range_type);
r->pymech_ = PyObject_New(NPyMechObj, pmech_generic_type);
r->pymech_ = new_pymechobj();

Check warning on line 1964 in src/nrnpython/nrnpy_nrn.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_nrn.cpp#L1964

Added line #L1964 was not covered by tests
r->pymech_->pyseg_ = self;
Py_INCREF(self);
r->sym_ = sym;
Expand Down
Loading