Skip to content

Commit 4ed27c7

Browse files
committed
PYCBC-1649: Transactions - fix memory leak when creating
TransactionGetResult Changes ======= * Move transaction_get_result struct to use std::unique_ptr instead of raw pointer * Only set the transaction_get_result's TransactionGetResult when necessary Change-Id: I00398c96d40fc3dc9dad18f1a62be92551039d0d Reviewed-on: https://review.couchbase.org/c/couchbase-python-client/+/222085 Reviewed-by: Dimitris Christodoulou <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent 03c270e commit 4ed27c7

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/transactions/transactions.cxx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ static PyTypeObject transaction_query_options_type = init_transaction_query_opti
519519
void
520520
pycbc_txns::transaction_get_result__dealloc__(pycbc_txns::transaction_get_result* result)
521521
{
522-
delete result->res;
522+
result->res.reset();
523523
Py_TYPE(result)->tp_free((PyObject*)result);
524524
CB_LOG_DEBUG("dealloc transaction_get_result");
525525
}
@@ -593,7 +593,6 @@ PyObject*
593593
pycbc_txns::transaction_get_result__new__(PyTypeObject* type, PyObject*, PyObject*)
594594
{
595595
auto self = reinterpret_cast<pycbc_txns::transaction_get_result*>(type->tp_alloc(type, 0));
596-
self->res = new tx_core::transaction_get_result();
597596
return reinterpret_cast<PyObject*>(self);
598597
}
599598

@@ -996,12 +995,11 @@ handle_returning_void(PyObject* pyObj_callback,
996995
}
997996

998997
void
999-
handle_returning_transaction_get_result(
1000-
PyObject* pyObj_callback,
1001-
PyObject* pyObj_errback,
1002-
std::shared_ptr<std::promise<PyObject*>> barrier,
1003-
std::exception_ptr err,
1004-
std::optional<couchbase::core::transactions::transaction_get_result> res)
998+
handle_returning_transaction_get_result(PyObject* pyObj_callback,
999+
PyObject* pyObj_errback,
1000+
std::shared_ptr<std::promise<PyObject*>> barrier,
1001+
std::exception_ptr err,
1002+
std::optional<tx_core::transaction_get_result> res)
10051003
{
10061004
// TODO: flesh out transaction_get_result and exceptions...
10071005
auto state = PyGILState_Ensure();
@@ -1032,9 +1030,7 @@ handle_returning_transaction_get_result(
10321030
pyObj_get_result =
10331031
PyObject_CallObject(reinterpret_cast<PyObject*>(&transaction_get_result_type), nullptr);
10341032
auto result = reinterpret_cast<pycbc_txns::transaction_get_result*>(pyObj_get_result);
1035-
// now lets copy it in
1036-
// TODO: ideally we'd have a move constructor for transaction_get_result, but for now...
1037-
result->res = new tx_core::transaction_get_result(res.value());
1033+
result->res = std::make_unique<tx_core::transaction_get_result>(std::move(res.value()));
10381034
}
10391035

10401036
if (nullptr == pyObj_callback) {

src/transactions/transactions.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ struct transaction_context {
140140
};
141141

142142
struct transaction_get_result {
143-
PyObject_HEAD tx_core::transaction_get_result* res;
143+
PyObject_HEAD std::unique_ptr<tx_core::transaction_get_result> res;
144144
};
145145

146146
struct transaction_query_options {

0 commit comments

Comments
 (0)