title: Rcpp Objects seem to be destroyed by R's garbage collector
body: <p>I have a R script that creates Rcpp objects of the same class that are constructed from different underlying data. Then <code>lapply()</code> is used to call their method and the data is put into a list.</p>
  
  <p>But after running for a while, it crashes with the following error. To me it seems to be a garbage collector issue. </p>
  
  <pre><code>no function to return from, jumping to top level
  1: foo(items)
   2: lapply(items) {
   3: FUN(X[[1]], ...)
   4: lapply(1:5, function(i) {
      myRcppModule::bar(item)
   5: FUN(1:4[[2]], ...)
   6: myRcppModule::bar(item)
   7: new(Rcpp::Module("myRcppMod", PACKAGE = "MyRCPP_MOD")$MyRcppModWrapper, list(item)
   8: getClass(Class, where = topenv(parent.frame()))
   9: .getClassFromCache(Class, where)
   10: Rcpp::Module("myRcppMod", PACKAGE = "MyRCPP_MOD")$MyRcppModWrapper
  11: .getModulePointer(x)
  12: Module(module, mustStart = TRUE)
  13: methods::setRefClass(clname, fields = fields, contains = "C++Object", methods = methods, where = where)
  14: setClass(Class, contains = superClasses, where = where, ...)
  15: is(try(setIs(Class, class2, classDef = classDef, where = where)), "try-error")
  16: try(setIs(Class, class2, classDef = classDef, where = where))
  17: tryCatch(expr, error = function(e) {
      call &lt;- conditionCall(e)
      if (!is.null(call)) {
          if (identical(call[[1]], quote(doTryCatch)))
              c
  18: tryCatchList(expr, classes, parentenv, handlers)
  19: tryCatchOne(expr, names, parentenv, handlers[[1]])
  20: doTryCatch(return(expr), name, parentenv, handler)
  21: setIs(Class, class2, classDef = classDef, where = where)
  22: completeExtends(classDef, class2, obj, where = where)
  23: .walkClassGraph(ClassDef, "contains", where, attr(ext, "conflicts"))
  24: .transitiveExtends(className, by, ext[[i]], exti, strictBy)
  25: .combineExtends(byExt, toExt, by, to, strictBy)
  26: (function (x)
  x$.self$finalize())(&lt;environment&gt;)
  27: x$.self$finalize
  28: envRefInferField(x, what, getClass(class(x)), selfEnv)
  </code></pre>
  
  <p>A snippet looks like the following</p>
  
  <pre><code>foo &lt;- function(items) {
      lapply(
          items,
          function(item) {
              data &lt;- lapply(
                  1:5,
                  function(i) {
                      obj &lt;- myRcppModule::bar(item)
                      obj$getData()
              })
      })
  }
  
  while(TRUE) {
      items &lt;- some_vector
      foo(items)
  }
  </code></pre>
  
  <p>The script can run much longer without crashing if I call gc() after iteration. This makes me think that I am doing something wrong with the memory. Could a <code>obj</code> be freed by <code>gc</code> right before <code>getData()</code> is called?</p>
  
  <p>Please help. Thanks in advance.</p>
  
  <pre><code>foo &lt;- function(items) {
      lapply(
          items,
          function(item) {
              data &lt;- lapply(
                  1:5,
                  function(i) {
                      obj &lt;- myRcppModule::bar(item)
                      obj$getData()
              })
              gc(TRUE)
      })
  }
  
  while(TRUE) {
      items &lt;- some_vector
      foo(items)
  }
  </code></pre>
  
  <p>The Rcpp code for object creation is the following, where the <code>MyRcppModWrapper</code> is a normal C++ class.</p>
  
  <pre><code>MyRcppModWrapper* createMyRcppMod(SEXP argsexp)
  {
      try {
          Rcpp::List args = Rcpp::as&lt;Rcpp::List&gt;(argsexp);
          const auto item = Rcpp::as&lt;std::string&gt;(args["item"]);
  
          auto obj = new MyRcppModWrapper(item);
  
          return obj;
      } catch(const std::exception&amp; e) {
          forward_exception_to_r(e);
      }
  
      return nullptr;
  }
  
  std::array&lt;double&gt; getData(MyRcppModWrapper* self)
  {
          try {
              return self-&gt;getData();
          } catch (const std::exception&amp; e) {
  
  </code></pre>