title: How to handle C++ internal data structure in R in order to allow save/load?
body: <p>In R, I am using an internal C++ structure to store the data (Using R_ExternalPtr). The data can then be processed using various functions. Here is a simple example (the real data structure is much more complex):</p>
  
  <pre><code>#include &lt;Rinternals.h&gt;
  class MyObject{
          public:
          int type;
          MyObject(int t):type(t){}
  };
  
  
  void finalizeMyObject(SEXP ptr){
      MyObject * mo= static_cast&lt;MyObject *&gt;(R_ExternalPtrAddr(ptr));
      delete mo;
  }
  
  extern "C" {
  
      SEXP CreateObject(SEXP type) {
          SEXP ans;
          int nb = length(type);
          PROTECT(ans=allocVector(VECSXP, nb));
          int * typeP = INTEGER(type);
          SEXP cl;
          PROTECT(cl = allocVector(STRSXP, 1));
          SET_STRING_ELT(cl, 0, mkChar("myObject"));
          for(int i=0; i&lt;nb; i++){
              MyObject * mo = new MyObject(typeP[i]);
              SEXP tmpObj = R_MakeExternalPtr(mo, R_NilValue, R_NilValue);
              R_RegisterCFinalizerEx(tmpObj, (R_CFinalizer_t) finalizeMyObject, TRUE);
  
              classgets(tmpObj, cl);
              SET_VECTOR_ELT(ans, i, tmpObj);//Put in vector
          }
          UNPROTECT(2);
          return ans;
      }
  
      SEXP printMyObject(SEXP myObjects){
          int nb = length(myObjects);
          for(int i=0; i&lt;nb; i++){
              SEXP moS=VECTOR_ELT(myObjects, i);
              MyObject * mo= static_cast&lt;MyObject *&gt;(R_ExternalPtrAddr(moS));
              Rprintf("%d\n", mo-&gt;type);
          }
          return R_NilValue;
      }
  }
  </code></pre>
  
  <p>Once the internal data structure is built, several functions can be called to compute different statistics. The above function printMyObject offer an example of such function.</p>
  
  <p>The problem arises when trying to save this internal structure. It seems to save the pointer addresses. When the objects are reloaded, we get a segfault. Here is an example R code (suppose that myobject.cpp contains the code above and was compiled to myobject.dll)</p>
  
  <pre><code>dyn.load("myobject.dll")
  ## Create the internal data structure
  xx &lt;- .Call("CreateObject", 1:10)
  ##Everything is fine
  .Call("printMyObject", xx)
  ## Save it, no errors
  save(xx, file="xx.RData")
  ## remove all objects
  rm(list=ls())
  ## Load the data
  load(file="xx.RData")
  ##segfault
  .Call("printMyObject", xx)
  </code></pre>
  
  <p>My question is: what is the best way to handle this correctly? I thought about some strategies, but except for the first one, I do not know how it can be done (and if it is possible to do it):</p>
  
  <ul>
  <li>Do not return the internal object. Users always have R structures that are internally converted (for each function call, such as print and so on) to the internal C++ structure. Pros: data can be saved without segfault. Cons: this is very inefficient especially when there are a lot of data. </li>
  <li>Store the object as R <strong>and</strong> C++ structure and try to figure out (I do not know how) if the C++ structure is corrupted to rebuild it. Pros: internal data structure is built only once per session. Cons: not the best for memory issues (data stored twice).</li>
  <li>Use only C++ structure and never save/load (actual solution used). The problem is that there are no error messages, but just a segfault.</li>
  <li>Find a way to serialize the internal C++ object when R save/load functions are used.</li>
  </ul>
  
  <p>Any idea/suggestions are very welcome.</p>