Skip to content
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

Memory leak with reassigning cpp11::writable::list, and maybe others #338

Closed
krlmlr opened this issue Oct 23, 2023 · 1 comment · Fixed by #366
Closed

Memory leak with reassigning cpp11::writable::list, and maybe others #338

krlmlr opened this issue Oct 23, 2023 · 1 comment · Fixed by #366

Comments

@krlmlr
Copy link
Member

krlmlr commented Oct 23, 2023

Reassigning an auto variable generated from a call to a function that returns cpp11::writable::list appears to leak memory.

Downstream: r-dbi/RMariaDB#309.

cpp11::cpp_source(quiet = FALSE, cxx_std = "CXX17", code = "
#include <cpp11.hpp>

cpp11::writable::list test_list() {
  return cpp11::writable::list(10000);
}

[[cpp11::register]]
cpp11::list test_list3() {
  auto out = test_list();
  out = test_list();
  out = test_list();
  return out;
}
")
#> ℹ 1 functions decorated with [[cpp11::register]]
#> using C++ compiler: ‘Apple clang version 15.0.0 (clang-1500.0.40.1)’
#> using C++17
#> using SDK: ‘MacOSX14.0.sdk’
#> ccache clang++ -arch arm64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/include" -DNDEBUG -I'/Users/kirill/Library/R/arm64/4.3/library/cpp11/include'  -I/opt/homebrew/include    -fPIC  -O0 -g -Wmacro-redefined -Wno-everything -c /private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpsq7RPu/file180e56b92260/src/code_180e147ff91b.cpp -o /private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpsq7RPu/file180e56b92260/src/code_180e147ff91b.o
#> ccache clang++ -arch arm64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/include" -DNDEBUG -I'/Users/kirill/Library/R/arm64/4.3/library/cpp11/include'  -I/opt/homebrew/include    -fPIC  -O0 -g -Wmacro-redefined -Wno-everything -c /private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpsq7RPu/file180e56b92260/src/cpp11.cpp -o /private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpsq7RPu/file180e56b92260/src/cpp11.o
#> ccache clang++ -arch arm64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib -L/opt/homebrew/lib -o /private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpsq7RPu/file180e56b92260/src/code_180e147ff91b.so /private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpsq7RPu/file180e56b92260/src/code_180e147ff91b.o /private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpsq7RPu/file180e56b92260/src/cpp11.o -F/Library/Frameworks/R.framework/Versions/4.3-arm64 -framework R -Wl,-framework -Wl,CoreFoundation
#> ld: warning: -single_module is obsolete
#> ld: warning: -multiply_defined is obsolete

gc()
#>           used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
#> Ncells  845351 45.2    1449535 77.5         NA  1449535 77.5
#> Vcells 1511050 11.6    8388608 64.0      24576  2326823 17.8

for (i in 1:3) {
  for (j in 1:100) {
    test_list3()
  }
  print(gc())
}
#>           used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
#> Ncells  846348 45.2    1449535 77.5         NA  1449535 77.5
#> Vcells 3512756 26.9    8388608 64.0      24576  4541845 34.7
#>           used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
#> Ncells  846748 45.3    1449535 77.5         NA  1449535 77.5
#> Vcells 5512779 42.1   10146329 77.5      24576  6512871 49.7
#>           used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
#> Ncells  847150 45.3    1449535 77.5         NA  1449535 77.5
#> Vcells 7512799 57.4   12255594 93.6      24576  8512899 65.0

Created on 2023-10-23 with reprex v2.0.2

@pachadotdev
Copy link
Contributor

here is the full valgrind output of the previous example

valgrind.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants