File tree Expand file tree Collapse file tree 1 file changed +9
-4
lines changed Expand file tree Collapse file tree 1 file changed +9
-4
lines changed Original file line number Diff line number Diff line change @@ -270,21 +270,26 @@ class RefBase : protected Finalizer, RefTracker {
270
270
271
271
protected:
272
272
inline void Finalize (bool is_env_teardown = false ) override {
273
- // Force deferring behavior if the finalizer happens to delete this
274
- // reference.
273
+ // During environment teardown we have to convert a strong reference to
274
+ // a weak reference to force the deferring behavior if the user's finalizer
275
+ // happens to delete this reference so that the code in this function that
276
+ // follows the call to the user's finalizer may safely access variables from
277
+ // this instance.
275
278
if (is_env_teardown && RefCount () > 0 ) _refcount = 0 ;
276
279
277
280
if (_finalize_callback != nullptr ) {
278
281
_env->CallFinalizer (_finalize_callback, _finalize_data, _finalize_hint);
282
+ // This ensures that we never call the finalizer twice.
283
+ _finalize_callback = nullptr ;
279
284
}
280
285
281
286
// this is safe because if a request to delete the reference
282
287
// is made in the finalize_callback it will defer deletion
283
288
// to this block and set _delete_self to true
284
- _finalize_ran = true ;
285
-
286
289
if (_delete_self || is_env_teardown) {
287
290
Delete (this );
291
+ } else {
292
+ _finalize_ran = true ;
288
293
}
289
294
}
290
295
You can’t perform that action at this time.
0 commit comments