mcdougallab / matlabneuroninterface

Interface for connecting NEURON and MATLAB
BSD 3-Clause "New" or "Revised" License
5 stars 1 forks source link

Methods returning new objects #66

Closed edovanveen closed 1 year ago

edovanveen commented 1 year ago

How do we deal with methods that return new objects? For example Vector.c() and (probably) ParallelContext.gid_connect(i, cell).

ramcdougal commented 1 year ago

I think the underlying issue is that we need to increment the returned object's refcount in +neuron/+stack/hoc_pop.m's hoc_pop. I think the reason we didn't run into this before was that the refcount for things that already existed was non-zero and so they weren't getting garbage collected on the C-side. Incrementing refcount on return may also fix the underlying issue that led to #68 where MATLAB garbage collection was causing objects to go away when they shouldn't (because the refcount wasn't right).

For an example with Vector.c(), see https://github.com/mcdougallab/neuron-c-api-demos/blob/main/vector-dot-c/vector-dot-c.cpp The main function of interest is: call_object_method_that_returns_object

In particular, commenting out the new_ob_ptr->refcount++; line will lead to the Vector getting garbage collected and not being useable for printf.

edovanveen commented 1 year ago

Fixed in PR #47