Skip to content

Commit 497f7ba

Browse files
committed
Clear external pointer before calling finalizer
1 parent ee2e102 commit 497f7ba

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

inst/include/Rcpp/XPtr.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ void finalizer_wrapper(SEXP p) {
3636
if (TYPEOF(p) == EXTPTRSXP) {
3737
T* ptr = (T*) R_ExternalPtrAddr(p);
3838
RCPP_DEBUG_3("finalizer_wrapper<%s>(SEXP p = <%p>). ptr = %p", DEMANGLE(T), p, ptr)
39+
40+
// Clear before finalizing to avoid interesting behavior,
41+
// e.g. https://github.com/r-dbi/RPostgres/issues/167
42+
R_ClearExternalPtr(p);
43+
3944
Finalizer(ptr);
4045
}
4146
}
@@ -174,10 +179,10 @@ class XPtr :
174179
// Call the finalizer -- note that this implies that finalizers
175180
// need to be ready for a NULL external pointer value (our
176181
// default C++ finalizer is since delete NULL is a no-op).
182+
// This clears the external pointer just before calling the finalizer,
183+
// to avoid interesting behavior with co-dependent finalizers,
184+
// e.g. https://github.com/r-dbi/RPostgres/issues/167.
177185
finalizer_wrapper<T,Finalizer>(Storage::get__());
178-
179-
// Clear the external pointer
180-
R_ClearExternalPtr(Storage::get__());
181186
}
182187
}
183188

0 commit comments

Comments
 (0)