Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:05:05+00:00 2026-06-05T20:05:05+00:00

Having some trouble with R’s garbage collection, when passing objects to C++. We have

  • 0

Having some trouble with R’s garbage collection, when passing objects to C++.

We have the following scenario:

  1. we create an anonymous function in R, and pass it to C++ code (via .Call())
  2. the C++ code stores the R function object for later use (as a SEXP type) and returns
  3. later on, some other C++ code invokes said R function object using R_tryEval()

Between steps 2 and 3, the R function object appears to get garbage-collected by R. This leads to a crash because R_tryEval() tries to execute something that no longer represents a valid R function object. That’s fair, as we haven’t done anything to tell R that the function object is still in use…

With that in mind:

  • is there a way, from the C++ code, to mark the R function object as being in-use (such that it doesn’t get gc’d)?
  • or is there a safe way to duplicate the R function object, within the C++ code, and manually dispose of it after we invoke R_tryEval()?

(As far as I understand, the PROTECT()/UNPROTECT() macros are not an option here because those are supposed to balance out within the same scope. As in, we can’t call PROTECT() when the function is first passed to C++ and then later call UNPROTECT() after it has been executed.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:05:06+00:00Added an answer on June 5, 2026 at 8:05 pm

    I think you’re looking for

    /* preserve objects across GCs */
    void R_PreserveObject(SEXP);
    void R_ReleaseObject(SEXP);
    

    in the R_internals.h header.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having some trouble writing a getstring function, this is what I have so
having some trouble passing a function as a parameter of another function... ERROR: Error
Having some trouble passing values from one class to another. I basically have a
im having some trouble with the following code: Ext.define('...controller...', { extend: 'Ext.app.Controller', init: function()
Having some trouble with what should be a very simple scenario. For example purposes,
I´m having some trouble retrieving a collection of strings in a projection: say that
I having some trouble figuring this problem out where I have one button which
Im having some trouble with jQuery's slideToggle function in IE8 for some reason the
I'm having some trouble opening an existing blackberry eclipse project I have to work
Having some trouble with some SQL. Take the following result for instance: LOC_CODE CHANNEL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.