Skip to content

Commit

Permalink
Fix memory leak for mpfr/mpc cache (amend 7351e2e)
Browse files Browse the repository at this point in the history
Closes #511
  • Loading branch information
skirpichev committed Sep 13, 2024
1 parent 2940505 commit 3286b7a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/gmpy2_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,15 @@ GMPy_MPFR_New(mpfr_prec_t bits, CTXT_Object *context)
if (global.in_gmpympfrcache) {
result = global.gmpympfrcache[--(global.in_gmpympfrcache)];
Py_INCREF((PyObject*)result);
mpfr_set_prec(result->f, bits);
}
else {
result = PyObject_New(MPFR_Object, &MPFR_Type);
if (result == NULL) {
return NULL;
}
mpfr_init2(result->f, bits);
}
mpfr_init2(result->f, bits);
result->hash_cache = -1;
result->rc = 0;
return result;
Expand Down Expand Up @@ -641,14 +642,21 @@ GMPy_MPC_New(mpfr_prec_t rprec, mpfr_prec_t iprec, CTXT_Object *context)
if (global.in_gmpympccache) {
result = global.gmpympccache[--(global.in_gmpympccache)];
Py_INCREF((PyObject*)result);
if (rprec == iprec) {
mpc_set_prec(result->c, rprec);
}
else {
mpc_clear(result->c);
mpc_init3(result->c, rprec, iprec);
}
}
else {
result = PyObject_New(MPC_Object, &MPC_Type);
if (result == NULL) {
return NULL;
}
mpc_init3(result->c, rprec, iprec);
}
mpc_init3(result->c, rprec, iprec);
result->hash_cache = -1;
result->rc = 0;
return result;
Expand Down

0 comments on commit 3286b7a

Please sign in to comment.