diff --git a/python/objToJSON.c b/python/objToJSON.c index 743e6f29..b3d5cc2c 100644 --- a/python/objToJSON.c +++ b/python/objToJSON.c @@ -648,6 +648,7 @@ static void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc, JSONObject if (newObj) { PRINTMARK(); + Py_XDECREF(pc->newObj); obj = pc->newObj = newObj; level += 1; goto BEGIN; diff --git a/tests/test_ujson.py b/tests/test_ujson.py index b0b39479..b20404ce 100644 --- a/tests/test_ujson.py +++ b/tests/test_ujson.py @@ -974,6 +974,28 @@ def test_issue_334(indent): ujson.dumps(a, indent=indent) +@pytest.mark.skipif( + hasattr(sys, "pypy_version_info"), reason="PyPy uses incompatible GC" +) +def test_default_ref_counting(): + class DefaultRefCountingClass: + def __init__(self, value): + self._value = value + + def convert(self): + if self._value > 1: + return type(self)(self._value - 1) + return 0 + + import gc + + gc.collect() + ujson.dumps(DefaultRefCountingClass(3), default=lambda x: x.convert()) + assert not any( + type(o).__name__ == "DefaultRefCountingClass" for o in gc.get_objects() + ) + + """ def test_decode_numeric_int_frc_overflow(): input = "X.Y"