Some deferred observers get destroyed during updates #2409
Merged
+38
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I noticed an issue with deferred updates in that elements of the
deferredObservers_set can become null during theObservableSettings::enableUpdates()call. The problem is that at the start of that method hereQuantLib/ql/patterns/observable.cpp
Line 30 in ecefd25
updatesDeferred_is set to false. Then, there is the loop overdeferredObservers_callingdeferredObserver->update()on each. It is during this loop, that the call toupdate()for adeferredObserver"earlier" in thedeferredObservers_set leads to the destruction of adeferredObserver"later" in thedeferredObservers_set. The destructor of the laterdeferredObserveris called andObserver::~Observer()andobservable->unregisterObserver(this)are calledQuantLib/ql/patterns/observable.hpp
Line 220 in ecefd25
The issue is that
updatesDeferred_is at this pointfalseand hence the line hereQuantLib/ql/patterns/observable.hpp
Line 199 in ecefd25
deferredObserverthat is destructed is left in thedeferredObservers_set.I added a test in commit c2b5cc8 to demonstrate the issue. In commit ad90c6f, I added a fix. The fix is based on the proposal by @pcaspers here. Let me know what you think. I can make amendments if you think the fix can be improved.