kaskr / adcomp

AD computation with Template Model Builder (TMB)
Other
178 stars 81 forks source link

R-devel (r86800) breaks TMB:::isNullPointer #395

Closed jaganmn closed 5 months ago

jaganmn commented 5 months ago
## R 4.4.1
> TMB:::isNullPointer(NULL)
[1] FALSE
## R-devel (since r86800)
> TMB:::isNullPointer(NULL)
Error in TMB:::isNullPointer(NULL) : 
  R_ExternalPtrAddr: argument of type NILSXP is not an external pointer

The change seems to at least cause problems with "reports":

dll <- tempfile()
cpp <- paste0(dll, ".cpp")
cat(file = cpp, "#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
    vector<Type> x(1); x(0) = Type(0.0); REPORT(x);
    return Type(0.0);
}
")
TMB::compile(cpp)
dyn.load(paste0(dll, .Platform[["dynlib.ext"]]))
obj <- TMB::MakeADFun(data = list(), parameters = list(), type = "Fun",
                      checkParameterOrder = FALSE, DLL = basename(dll))
obj[["report"]]()
> obj[["report"]]()
Error in isNullPointer(ADFun$ptr) : 
  R_ExternalPtrAddr: argument of type NILSXP is not an external pointer
> traceback()
3: isNullPointer(ADFun$ptr)
2: f(par, order = 0, type = "double")
1: obj[["report"]]()

A patch along these lines should fix it ... ?

diff --git a/TMB/src/utils.c b/TMB/src/utils.c
index 382c1338..3a835b78 100644
--- a/TMB/src/utils.c
+++ b/TMB/src/utils.c
@@ -31,5 +31,5 @@ SEXP setxslot(SEXP x, SEXP y){

 /* Is external pointer nil ? */
 SEXP isNullPointer(SEXP pointer) {
-  return ScalarLogical(!R_ExternalPtrAddr(pointer));
+  return ScalarLogical(TYPEOF(pointer) == EXTPTRSXP && !R_ExternalPtrAddr(pointer));
 }

Well, you could instead test in R with typeof(ADFun$ptr) == "externalptr" && isNullPointer(ADFun$ptr) ...

kaskr commented 5 months ago

Thanks @jaganmn !

jaganmn commented 5 months ago

Thanks - CRAN will probably want a patched version of TMB in the next 1-2 weeks, since at least one reverse dependency of TMB is now failing its check: https://cran.r-project.org/web/checks/check_results_epigrowthfit.html

lentinj commented 5 months ago

gadget3 is also failing checks due to the above: https://cran.r-project.org/web/packages/gadget3/index.html - thanks for already applying a fix.

A new release next week would be appreciated, although we could also remove the offending examples if need be.

kaskr commented 5 months ago

@jaganmn and @lentinj FYI I've just subitted TMB-1.9.13 to CRAN.