mcdougallab / matlabneuroninterface

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

Deleting NEURON Objects #68

Closed edovanveen closed 1 year ago

edovanveen commented 1 year ago

The user currently has to explicitly delete any NEURON Objects by running Object.delete_nrn_obj() in MATLAB. If this method were named Object.delete() it would lead to unexpected behaviour, such as deleting a vector upon calling Vector.append() without catching the output.

Instead we could keep track of whether the MATLAB Object is the owner of the NEURON Object, and if so, run Object.delete_nrn_obj() upon Object.delete().

ramcdougal commented 1 year ago

We should only be telling NEURON to delete the object if its reference count goes to 0.

Presumably deleting it in MATLAB should simply decrement its reference count by 1.

edovanveen commented 1 year ago

Removing clibRelease was causing crashes on reruns, so I put it back for now; it is only run if the MATLAB Object owns the NEURON Object (see Object.objowner and Object.delete() in PR #47). In the future it is advisable to look at this issue again and see if it also works to simply decrease the refcount.

edovanveen commented 1 year ago

I got rid of the objowner nonsense; currently deleting an Object looks like the code below. I still have to do clibRelease because otherwise some examples crash if run in succession.

        function delete(self)
        % Decrease refcount by 1; if refcount is 0, release the C++ Object.
        %   delete()

            self.obj.refcount = self.obj.refcount - 1;

            % We need to do this, or else we get crashes.
            if self.obj.refcount == 0
                clib.neuron.hoc_obj_unref(self.obj);
                clibRelease(self.obj);
            end