Skip to content

Commit

Permalink
Added dereferencing for Python in Dense output
Browse files Browse the repository at this point in the history
  • Loading branch information
jrenaud90 committed Nov 13, 2024
1 parent 8063fe2 commit e7e2459
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 97 deletions.
4 changes: 2 additions & 2 deletions CyRK/cy/cysolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ CySolverBase::CySolverBase(
CySolverBase::~CySolverBase()
{
this->storage_ptr = nullptr;
if (this->use_pysolver)
if (this->deconstruct_python)
{
// Decrease reference count on the cython extension class instance
Py_XDECREF(this->cython_extension_class_instance);
Expand Down Expand Up @@ -592,8 +592,8 @@ void CySolverBase::set_cython_extension_instance(PyObject* cython_extension_clas
}
else
{
// TODO: Do we need to decref this at some point? During CySolver's deconstruction?
Py_XINCREF(this->cython_extension_class_instance);
this->deconstruct_python = true;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions CyRK/cy/cysolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class CySolverBase {
bool reset_called = false;
bool capture_extra = false;
bool user_provided_max_num_steps = false;
bool deconstruct_python = false;

// Dense (Interpolation) Attributes
bool use_dense_output = false;
Expand Down
14 changes: 12 additions & 2 deletions CyRK/cy/dense.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include "dense.hpp"


// Constructors
CySolverDense::CySolverDense(
int integrator_int,
double t_old,
Expand Down Expand Up @@ -60,12 +60,22 @@ CySolverDense::CySolverDense(
// Make a strong reference to the python class (if this dense output was built using the python hooks).
if (cython_extension_class_instance)
{
// TODO: Do we need to decref this at some point? During CySolver's deconstruction?
Py_XINCREF(this->cython_extension_class_instance);
this->deconstruct_python = true;
}

}

// Destructors
CySolverDense::~CySolverDense()
{
if (this->deconstruct_python)
{
// Decrease reference count on the cython extension class instance
Py_XDECREF(this->cython_extension_class_instance);
}
}

void CySolverDense::call(double t_interp, double* y_interp_ptr)
{
double step_factor = (t_interp - this->t_old) / this->step;
Expand Down
3 changes: 2 additions & 1 deletion CyRK/cy/dense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CySolverDense
double* cysolver_y_now_ptr = nullptr;
double* cysolver_dy_now_ptr = nullptr;
PyObject* cython_extension_class_instance = nullptr;
bool deconstruct_python = false;

// Time step info
double step = 0.0;
Expand All @@ -59,7 +60,7 @@ class CySolverDense
protected:

public:
virtual ~CySolverDense() { };
virtual ~CySolverDense();
CySolverDense() {};
CySolverDense(
int integrator_int,
Expand Down
Loading

0 comments on commit e7e2459

Please sign in to comment.