[Rcpp-devel] idiom for printing a message when a destructor is invoked

Dirk Eddelbuettel edd at debian.org
Fri Dec 16 00:23:54 CET 2011


On 15 December 2011 at 16:48, Douglas Bates wrote:
| I am communicating back and forth between R reference classes and C++
| classes through external pointers and it seems that the C++ object is
| being destroyed prematurely.  I will look more carefully at the
| Rcpp::Xptr class to ensure that the C++ object is preserved until the
| R object is garbage collected but, for now, I would like to pinpoint
| when the destructor for the C++ class is being called on the object.
| I'm sure there must be an idiom for inserting something like
| 
| Rcpp::Rcout << "Destructor being called on object of class foo" << std::endl;
| 
| in a destructor then chaining to the standard destructor but I can see
| anything like that right now.  That is, I don't know how to chain to
| the standard destructor.
| 
| Any suggestions?

Not sure I fully understand. What is "the standard destructor"? Are you
referring to RObject?  If you don't define one, a default is created. That
latter one will be without your debug info, so I commonly just define one.
Eg when revisiting RInside a few days ago, I noticed it still had


RInside::~RInside() {           // now empty as MemBuf is internal
    logTxt("RInside::dtor BEGIN", verbose);
    // ... other stuff deleted
    logTxt("RInside::dtor END", verbose);
}

and logTxt is a little helper normally defined as a null-op that the compiler
os likely to throw out:

#ifdef logTxt
#undef logTxt
#endif
//#define logTxt(x, b) logTxtFunction(__FILE__, __LINE__, x, b);
#define logTxt(x, b)

and it uses

// simple logging help
inline void logTxtFunction(const char* file, const int line, const char* expression, const bool verbose) {
    if (verbose) {
        std::cout << file << ":" << line << " expression: " << expression << std::endl;
    }
}


Does this help?

Dirk

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list