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 {
270270
271271 protected:
272272 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.
275278 if (is_env_teardown && RefCount () > 0 ) _refcount = 0 ;
276279
277280 if (_finalize_callback != nullptr ) {
278281 _env->CallFinalizer (_finalize_callback, _finalize_data, _finalize_hint);
282+ // This ensures that we never call the finalizer twice.
283+ _finalize_callback = nullptr ;
279284 }
280285
281286 // this is safe because if a request to delete the reference
282287 // is made in the finalize_callback it will defer deletion
283288 // to this block and set _delete_self to true
284- _finalize_ran = true ;
285-
286289 if (_delete_self || is_env_teardown) {
287290 Delete (this );
291+ } else {
292+ _finalize_ran = true ;
288293 }
289294 }
290295
You can’t perform that action at this time.
0 commit comments