Index: Modules/_bsddb.c =================================================================== --- Modules/_bsddb.c (revision 42150) +++ Modules/_bsddb.c (working copy) @@ -266,6 +266,7 @@ typedef struct { PyObject_HEAD DB_TXN* txn; + PyObject *env; #ifdef HAVE_WEAKREF PyObject *in_weakreflist; /* List of weak references */ #endif @@ -928,6 +929,8 @@ DBTxnObject* self = PyObject_New(DBTxnObject, &DBTxn_Type); if (self == NULL) return NULL; + Py_INCREF(myenv); + self->env = (PyObject*)myenv; #ifdef HAVE_WEAKREF self->in_weakreflist = NULL; #endif @@ -943,6 +946,7 @@ * open DBTxns and closing any open DBs first. */ MYDB_END_ALLOW_THREADS; if (makeDBError(err)) { + PyObject_Del(self); self = NULL; } return self; @@ -973,6 +977,7 @@ } #endif + Py_DECREF(self->env); PyObject_Del(self); }