Skip to content

Fix memory leak with move assignment operator #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# cpp11 (development version)

* Fixed a memory leak with the `cpp11::writable::r_vector` move assignment
operator (#338).

* The approach for the protection list managed by cpp11 has been tweaked
slightly. In 0.4.6, we changed to an approach that creates one protection list
per compilation unit, but we now believe we've found an approach that is
Expand Down
19 changes: 19 additions & 0 deletions cpp11test/src/test-integers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "cpp11/doubles.hpp"
#include "cpp11/function.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/protect.hpp"
#include "cpp11/strings.hpp"

#include <testthat.h>
Expand Down Expand Up @@ -207,4 +208,22 @@ context("integers-C++") {
int y = NA_INTEGER;
expect_true(cpp11::is_na(y));
}

test_that("writable integer vector temporary isn't leaked (#338)") {
R_xlen_t before = cpp11::detail::store::count();

// +1 from `x` allocation
cpp11::writable::integers x(1);

// Calls move assignment operator `operator=(r_vector<T>&& rhs)`
// +1 from `rhs` allocation and move into `x`
// -1 from old `x` release
x = cpp11::writable::integers(1);

R_xlen_t after = cpp11::detail::store::count();

expect_true(before == 0);
// TODO: This should be 1 but writable vectors are being double protected
expect_true(after - before == 2);
}
}
19 changes: 19 additions & 0 deletions cpp11test/src/test-list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "cpp11/integers.hpp"
#include "cpp11/list.hpp"
#include "cpp11/logicals.hpp"
#include "cpp11/protect.hpp"
#include "cpp11/raws.hpp"
#include "cpp11/strings.hpp"

Expand Down Expand Up @@ -162,4 +163,22 @@ context("list-C++") {
expect_true(Rf_xlength(y) == 0);
expect_true(y != R_NilValue);
}

test_that("writable list vector temporary isn't leaked (#338)") {
R_xlen_t before = cpp11::detail::store::count();

// +1 from `x` allocation
cpp11::writable::list x(1);

// Calls move assignment operator `operator=(r_vector<T>&& rhs)`
// +1 from `rhs` allocation and move into `x`
// -1 from old `x` release
x = cpp11::writable::list(1);

R_xlen_t after = cpp11::detail::store::count();

expect_true(before == 0);
// TODO: This should be 1 but writable vectors are being double protected
expect_true(after - before == 2);
}
}
2 changes: 1 addition & 1 deletion inst/include/cpp11/r_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ inline r_vector<T>& r_vector<T>::operator=(r_vector<T>&& rhs) {
SEXP old_protect = protect_;

data_ = rhs.data_;
protect_ = detail::store::insert(data_);
protect_ = rhs.protect_;

detail::store::release(old_protect);

Expand Down
Loading