Skip to content
Back to formatted view

Raw Message

Message-ID: <CAFLer80OKhvuH2BRuC3qdTFN-2Kg6MPzh0A24NNBs+JMbO-fvQ@mail.gmail.com>
Date: 2014-11-12T13:36:09Z
From: Zheng Da
Subject: How to maintain memory in R extension

Hello,

I wrote a system to perform data analysis in C++. Now I am integrating
it to R. I need to allocate memory for my own C++ data structures,
which can't be represented by any R data structures. I create a global
hashtable to keep a reference to the C++ data structures. Whenever I
allocate one, I register it in the hashtable and return its key to the
R code. So later on, the R code can access the C++ data structures
with their keys.

The problem is how to perform garbage collection on the C++ data
structures. Once an R object that contains the key is garbage
collected, the R code can no longer access the corresponding C++ data
structure, so I need to deallocate it. Is there any way that the C++
code can get notification when an R object gets garbage collected? If
not, what is the usual way to manage memory in R extensions?

Thanks,
Da