[Rcpp-devel] Abort object construction, Exceptions

schattenpflanze at arcor.de schattenpflanze at arcor.de
Sat Apr 23 13:00:56 CEST 2011


Sometimes I would like to abort the construction of an object, e.g., 
when the parameters are not sane. In this cases, I do not want the 
Rcpp/S4 object to be created.
An example: The user types
 > a <- new(A, p1)
where A is my C++ class, exposed to R via Rcpp, and p1 is a parameter. 
If p1 is invalid (not in a certain range), I do _not_ want 'a' to exist 
after this line.

Currently, I check the parameter in the C++ constructor of A and throw 
an exception if it is invalid. This is the standard way of exiting a C++ 
constructor without actually constructing anything.  As expected, this 
leads to the output (preceded by my own, meaningful error message)
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) :
   std::exception

Afterwards, trying to access 'a' yields
 > a$p1
Error: Object 'a' not found.

So far, this is exactly what I want!

The problem here is:
Obviously some kind of object _is_ created. When the garbage collection 
finally kicks in at random times, I get the unexpected additional error
Error in x$.self$finalize() : Object '.pointer' not found.

How can I avoid this? Is there a better way to abort the construction of 
an object or to destroy the invalid S4 object immediately?


Best regards,
Peter






More information about the Rcpp-devel mailing list