Skip to content

Commit b89fcb9

Browse files
committed
check for the dummy pointer in CppObject__finalize instead of in R
1 parent 2f03363 commit b89fcb9

3 files changed

Lines changed: 15 additions & 11 deletions

File tree

‎ChangeLog‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
2026-10-22 Kevin Ushey <kevinushey@gmail.com>
22

3-
* R/Module.R (cpp_refMethods): Make finalize() a no-op for a module
4-
object that was never constructed (created without arguments for a class
5-
without a default constructor); it used to fail on the dummy pointer, and
6-
the failure surfaced as an error message at garbage collection
3+
* src/module.cpp (CppObject__finalize): Make finalize() a no-op for a
4+
module object that was never constructed (created without arguments for a
5+
class without a default constructor); it used to fail on the dummy
6+
pointer, and the failure surfaced as an error message at garbage
7+
collection
78
* inst/tinytest/test_module.R: Add test
89

910
2026-10-22 Michael Sumner <mdsumner@gmail.com>

‎R/Module.R‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,14 @@ method_wrapper <- function( METHOD, where ){
400400
## create a named list of the R methods to invoke C++ methods
401401
## from the C++ class with pointer xp
402402
cpp_refMethods <- function(CLASS, where) {
403-
# an object created without arguments for a class lacking a default
404-
# constructor holds the dummy pointer (see cpp_object_dummy), which is
405-
# not an external pointer: there is nothing to finalize for it
406403
finalizer <- eval( substitute(
407404
function(){
408-
if( ! identical( .pointer, dummy ) )
409-
.Call( CppObject__finalize, class_pointer , .pointer )
405+
.Call( CppObject__finalize, class_pointer , .pointer )
410406
},
411407
list(
408+
CLASS = CLASS@pointer,
412409
CppObject__finalize = CppObject__finalize,
413-
class_pointer = CLASS@pointer,
414-
dummy = .dummyInstancePointer
410+
class_pointer = CLASS@pointer
415411
)
416412
) )
417413
mets <- c(

‎src/module.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,14 @@ RCPP_FUN_4(SEXP, CppField__set, XP_Class cl, SEXP field_xp, SEXP obj, SEXP value
108108
cl->setProperty(field_xp, obj, value);
109109
return R_NilValue;
110110
}
111+
112+
extern SEXP rcpp_dummy_pointer;
113+
111114
RCPP_FUN_2(SEXP, CppObject__finalize, XP_Class cl, SEXP obj) {
115+
// an object created without arguments for a class lacking a default
116+
// constructor holds the dummy pointer: there is nothing to finalize
117+
if (obj == rcpp_dummy_pointer)
118+
return R_NilValue;
112119
cl->run_finalizer(obj);
113120
return R_NilValue;
114121
}

0 commit comments

Comments
 (0)