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

Douglas Bates bates at stat.wisc.edu
Fri Dec 16 18:35:10 CET 2011


On Thu, Dec 15, 2011 at 5:23 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> 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"?

I probably should have said "the default destructor".

> Are you referring to RObject?

No, I was thinking of the destructor for objects in a class that I
have generated.

It turns out that the problem was a dumb error on my part in that I
passed an SEXP that was supposed to be an external pointer but wasn't.
 Careless errors after a long day of programming.

This seems to indicate that as<Rcpp::XPtr<foo> > could do with a check
on the SEXP to make sure that it is indeed an external pointer.  I'll
take a look and see if I can determine where to make that check.

> 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