title: cuda-memcheck fails to detect memory leak in an R package
body: <p>I'm building CUDA-accelerated R packages, and I want to debug with <code>cuda-memcheck</code>. So in <a href="https://github.com/wlandau/rcppcuda/tree/deliberate_memory_leak" rel="nofollow">this minimal example</a> (in the <code>deliberate_memory_leak</code> GitHub branch), I create a memory leak in <code>someCUDAcode.c</code> by commenting out a necessary call to <code>cudaFree</code>. Then, I see if <code>cuda-memcheck</code> can find the leak.</p>
  
  <pre><code>$ cuda-memcheck --leak-check full  Rscript -e 'library(rcppcuda); hello()'
  ========= CUDA-MEMCHECK
  An object of class "MyClass"
  Slot "x":
   [1]  1  2  3  4  5  6  7  8  9 10
  
  Slot "y":
   [1]  1  2  3  4  5  6  7  8  9 10
  
  [1] "Object changed."
  An object of class "MyClass"
  Slot "x":
   [1] 500   2   3   4   5   6   7   8   9  10
  
  Slot "y":
   [1]    1 1000    3    4    5    6    7    8    9   10
  
  ========= LEAK SUMMARY: 0 bytes leaked in 0 allocations
  ========= ERROR SUMMARY: 0 errors
  $
  </code></pre>
  
  <p>No luck. Then I saw in the <a href="http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Using-valgrind" rel="nofollow">R extensions manual</a> that <code>R -d "valgrind --tool=memcheck --leak-check=full" --vanilla &lt; mypkg-Ex.R</code> is the right way to use <code>valgrind</code>. So I created a <code>test.R</code> file with <code>library(rcppcuda); hello()</code> and tried this.</p>
  
  <pre><code>R -d "cuda-memcheck --leak-check full" --vanilla &lt; test.R
  *** Further command line arguments ('--vanilla ') disregarded
  *** (maybe use 'run --vanilla ' from *inside* cuda-memcheck --leak-check full)
  
  ========= CUDA-MEMCHECK
  Fatal error: you must specify '--save', '--no-save' or '--vanilla'
  ========= LEAK SUMMARY: 0 bytes leaked in 0 allocations
  ========= ERROR SUMMARY: 0 errors
  </code></pre>
  
  <p>And finally,</p>
  
  <pre><code>$ cuda-memcheck --leak-check full R --vanilla &lt; test.R
  ========= CUDA-MEMCHECK
  
  R version 3.2.0 (2015-04-16) -- "Full of Ingredients"
  Copyright (C) 2015 The R Foundation for Statistical Computing
  Platform: x86_64-unknown-linux-gnu (64-bit)
  
  R is free software and comes with ABSOLUTELY NO WARRANTY.
  You are welcome to redistribute it under certain conditions.
  Type 'license()' or 'licence()' for distribution details.
  
    Natural language support but running in an English locale
  
  R is a collaborative project with many contributors.
  Type 'contributors()' for more information and
  'citation()' on how to cite R or R packages in publications.
  
  Type 'demo()' for some demos, 'help()' for on-line help, or
  'help.start()' for an HTML browser interface to help.
  Type 'q()' to quit R.
  
  &gt; library(rcppcuda); hello()
  An object of class "MyClass"
  Slot "x":
   [1]  1  2  3  4  5  6  7  8  9 10
  
  Slot "y":
   [1]  1  2  3  4  5  6  7  8  9 10
  
  [1] "Object changed."
  An object of class "MyClass"
  Slot "x":
   [1] 500   2   3   4   5   6   7   8   9  10
  
  Slot "y":
   [1]    1 1000    3    4    5    6    7    8    9   10
  
  &gt; 
  ========= LEAK SUMMARY: 0 bytes leaked in 0 allocations
  ========= ERROR SUMMARY: 0 errors
  $
  </code></pre>
  
  <p>Is it possible to make <code>cuda-memcheck</code> work for R packages? I know I thought I had figured it out <a href="http://r.789695.n4.nabble.com/cuda-memcheck-to-debug-CUDA-enabled-R-packages-td4691668.html#a4708479" rel="nofollow">here</a>, but back then, I didn't actually verify the answers that <code>cuda-memcheck</code> was giving me.</p>