parented_ptr: Assert parent at destruction instead of construction#2172
Merged
uklotzde merged 7 commits intomixxxdj:masterfrom Jun 17, 2019
Merged
Conversation
Previously, `parented_ptr` asserted on construction that the pointed-to object has a parent. However, sometimes one wants to create an object and assign it to a parent later. The [recommendation](https://www.mixxx.org/wiki/doku.php/coding_guidelines#pointer_object_lifetime_ownership) was to create a `unique_ptr` `p` instead, later assign the object to a parent and then create a new variable using `make_parented(p.release())`. This has however several drawbacks: * There is a short period where the object is already assigned to a parent but still owned by a `unique_ptr`. This can lead to a double free. * There are two pointers that and it depends on the code location which one is the correct one to access the object. * It is inconvenient to use, because one has to write an extra line where the `unique_ptr` is released. With this commit, `parented_ptr` asserts that the pointed-to object has a parent at the **destruction** of the `parented_ptr`. That means, that one can store the object using a `parented_ptr` a priori and there will be a warning (from the failing `DEBUG_ASSERT`) in case any object is leaked. Optionally, in a future PR, this behaviour can even be changed so as to delete the object that is going to be leaked. That is, the `parented_ptr` destructor wouldn't only `DEBUG_ASSERT` that the pointed-to object has a parent, but it would also delete it otherwise. Additional changes: * Made `parented_ptr` implicitly convertible to a raw pointer. The reasons why the standard library types are not convertible to a raw pointer don't apply to `parented_ptr`. Regarding the insufficient usage of `parented_ptr` in Mixxx at the moment, making it implicitly convertible will make its adoption easier because less code has to be changed. (No `.get()` everywhere.) * Added a constructor taking `nullptr_t`. * Added `noexcept` where applicable. * Formatted the code using `clang-format`.
uklotzde
reviewed
Jun 16, 2019
uklotzde
reviewed
Jun 16, 2019
uklotzde
reviewed
Jun 16, 2019
uklotzde
reviewed
Jun 16, 2019
Contributor
|
@haslersn Would you please sign our contributor agreement that allows us to publish your code: I confused you with another first-time contributor who provided the NixOS files a few days ago. |
uklotzde
reviewed
Jun 17, 2019
| public: | ||
| parented_ptr() noexcept = default; | ||
| parented_ptr() noexcept | ||
| : m_ptr{nullptr} { |
Contributor
There was a problem hiding this comment.
Good catch, I didn't notice either.
Contributor
|
LGTM. Thank you for improving the overall code quality and for digging deep to fix edge cases! |
Contributor
|
I'm getting build errors now with Clang 8.0.0 on Fedora 30: ... |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Previously,
parented_ptrasserted on construction that the pointed-to object has a parent. However, sometimes one wants to create an object and assign it to a parent later. The recommendation was to create aunique_ptrpinstead, later assign the object to a parent and then create a new variable usingmake_parented(p.release()).This has however several drawbacks:
unique_ptr. This can lead to a double free.unique_ptris released.With this commit,
parented_ptrasserts that the pointed-to object has a parent at the destruction of theparented_ptr. That means, that one can store the object using aparented_ptra priori and there will be a warning (from the failingDEBUG_ASSERT) in case any object is leaked.Optionally, in a future PR, this behaviour can even be changed so as to delete the object that is going to be leaked. That is, the
parented_ptrdestructor wouldn't onlyDEBUG_ASSERTthat the pointed-to object has a parent, but it would also delete it otherwise.Additional changes:
parented_ptrimplicitly convertible to a raw pointer. The reasons why the standard library types are not convertible to a raw pointer don't apply toparented_ptr. Regarding the insufficient usage ofparented_ptrin Mixxx at the moment, making it implicitly convertible will make its adoption easier because less code has to be changed. (No.get()everywhere.)nullptr_t.noexceptwhere applicable.clang-format.