<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p> </p>
<div class="moz-text-html" lang="x-unicode">
<p>Dear all,</p>
I have a question about <i>R_PreserveObject </i>and<i>
R_ReleaseObject </i>and<i> </i>thought this would be good
place to post it.
<br>
I apologize if the question is not suitable or naive.<br>
<br>
Suppose I have something like the class below for which I use
Rcpp-modules to extend it to R (code not shown).<br>
I use "insert" to add key-value pair to a map and "remove" to
remove an entry. <br>
<br>
I am not having memory issues or so, but the solution seems wrong
to me and I am concerned that it causes problems.<br>
Could you please give me some advice if this implementation is
wrong or unefficient?<br>
<br>
Thank you in advance.<br>
Best,<br>
Simon<br>
<br>
<p><font face="Calibri">template<typename T><br>
class map<br>
{<br>
public:<br>
map() = default;<br>
<br>
size_t size()<br>
{<br>
return map_.size();<br>
}<br>
<br>
void insert(T& t, SEXP u)<br>
{<br>
SEXP s = Rf_duplicate(VECTOR_ELT(u, i));<br>
R_PreserveObject(s);<br>
<br>
map_.insert(std::pair<T, SEXP>(t[i], s));<br>
}<br>
<br>
void remove(T& t)<br>
{<br>
auto iter = map_.equal_range(t);<br>
for (auto it = iter.first; it != iter.second;
++it)<br>
{<br>
R_ReleaseObject(it->second);<br>
}<br>
map_.erase(t);<br>
}<br>
<br>
private:<br>
std::unordered_map<T, SEXP> map_;<br>
};</font><br>
</p>
<br>
<br>
</div>
</body>
</html>