Search code examples
rvalgrindrcppcran

Replicating CRAN valgrind issues


I am trying to fix some issue with my package CamelUp on CRAN. This package uses Rcpp to implement a board game. My recent CRAN submissions have come back with comments and output such as:

==32365== 16,591,624 (2,608,512 direct, 13,983,112 indirect) bytes in
20,379 blocks are definitely lost in loss record 3,036 of 3,036
==32365==    at 0x4838E86: operator new(unsigned long)
(/builddir/build/BUILD/valgrind-3.15.0/coregrind/m_replacemalloc/vg_replace_malloc.c:344)
==32365==    by 0x184ED3E5: Board::Board(Board const&)
(/tmp/CamelUp.Rcheck/00_pkg_src/CamelUp/src/Board.cpp:67)
...
==32365==    by 0x1853045D: Simulator::simulateDecision(bool, int)
(/tmp/CamelUp.Rcheck/00_pkg_src/CamelUp/src/Simulator.cpp:64)
==32365==    by 0x18536509: Rcpp::CppMethod2<Simulator, Rcpp::Vector<19,
Rcpp::PreserveStorage>, bool, int>::operator()(Simulator*, SEXPREC**)
(R-devel/site-library/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:195)
==32365==    by 0x18535B32:
Rcpp::class_<Simulator>::invoke_notvoid(SEXPREC*, SEXPREC*, SEXPREC**,
int) (R-devel/site-library/Rcpp/include/Rcpp/module/class.h:234)
==32365==    by 0x17B9EBE1: CppMethod__invoke_notvoid(SEXPREC*)
(/tmp/RtmpKDbrDI/R.INSTALL1d1838b282b2/Rcpp/src/module.cpp:220)

I'm having trouble replicating these errors and I'm wondering if there is a straightforward way to use valgrind with my package to reproduce these errors. I've tried running locally with valgrind but couldn't get the track origins option to work and make it clear where these errors were in my code. I have also tried using Travis-CI with the following .travis.yml file:

language: r
cache: packages
r_check_args: '--use-valgrind'
addons:
  apt:
    packages:
      - valgrind
r:
  - oldrel
  - release
  - devel
env:
  - VALGRIND_OPTS='--tool=memcheck --memcheck:leak-check=full --track-origins=yes'

I'm hoping there is a way to replicate these errors so I can fix them.


Solution

  • I successfully used Docker to run valgrind for my tests, but I ended up deciding that the best way to integrate this testing was with Travis-CI. My .travis.yml file looks like this:

    language: r
    cache: packages
    addons:
      apt:
        packages:
          - valgrind
    r:
      - oldrel
      - release
      - devel
    
    after_success:
      - R -e "install.packages('${PKG_TARBALL}', repos=NULL, type='source')"
      - cd tests
      - R -d "valgrind --tool=memcheck --leak-check=full --track-origins=yes"  --vanilla < testthat.R
      - cd ..
    
    

    This now runs the tests with valgrind, although I have to manually scroll through to see the results. At some point I will make the build fail if there are memory leaks, but for now this works for me. I was mostly unfamiliar with Docker and valgrind, and didn't realize I need to be in the tests directory to run the tests in testthat.R.