frontend: Use qobject_cast when casting QObjects - #11784
Conversation
eb3f82d to
5582ea1
Compare
|
Converting to draft, as #11785 needs to be merged first. |
5582ea1 to
7808d32
Compare
7808d32 to
a9342f8
Compare
|
This PR now builds correctly |
3623034 to
c2398aa
Compare
PatTheMav
left a comment
There was a problem hiding this comment.
Overall I'm fine with this change, though it obviously touches a whole lot casts in the code that touch a whole lot of aspects of our frontend code, so this might be too late for the next version but should possibly merged after the next version is stabilised to give it some time to exist on the master branch.
@RytoEX WDYT?
I think this should be merged for 32.1, merge timing TBD. It does have a merge conflict though. |
c2398aa to
6789390
Compare
|
I've rebased this PR |
|
This requires a rebase. |
f6596c5 to
5c91477
Compare
Rebased and conflicts resolved, however I've also added a to-be-squashed commit with some additional casts that were either introduced in the rebase, or missed originally. |
5c91477 to
f6cc03e
Compare
sebastian-s-beckmann
left a comment
There was a problem hiding this comment.
I haven't gone through all the files (only about one third) but the idea should be clear.
There are still a lot of qobject_casts that should be static_casts. qobject_cast is only needed where it isn't clear (or assumed) that the cast succeeds. Especially in cases where the pointer is dereferenced immediately after the cast, qobject_cast doesn't make sense. Also in some cases, instead of casting from the main window it should just be OBSBasic::Get().
Also commit authorship is still changed.
Commit authorship is intentional since at this point it contains changes from both of us. Will address all the feedback. |
15aa946 to
87c6774
Compare
87c6774 to
677a0d8
Compare
PatTheMav
left a comment
There was a problem hiding this comment.
Even though it might seem superfluous to use qobject_cast or dynamic_cast without checking the result for nullptr after, even a missing nullptr check will create deterministic crash points (mainly when that pointer is dereferenced).
But as the crash by UpdateContextBar demonstrates, static_cast will produce a valid pointer and thus dereferencing will succeed and if both types happen to have identical member or method names (like GetSource) code might potentially succeed and produce corrupted data or crash at arbitrary points later.
Otherwise I agree that where no checks and balances are used (and code is pretty blasé about safety so far anyway) using static_cast directly seems more "honest".
8e97660 to
c788f7f
Compare
b6fa120 to
c1c6bed
Compare
When casting between QObjects, qobject_cast should always be used for dynamic casts. This increases performance as there is no RTTI (Run Time Type Information) with qobject_cast, like there is with dynamic_cast. In other cases where we are sure or assume the cast is valid, use static_cast. Using reinterpret_cast is bad practice, as you should use it in very specific cases. Co-Authored-By: Clayton Groeneveld <19962531+cg2121@users.noreply.github.com>
29ebe44 to
359c1fa
Compare
359c1fa to
f502b57
Compare
SourceTree is designed for SourceTreeModel and should avoid using any other model. Enforce this on the SourceTree itself
f502b57 to
4e8d78c
Compare
Description
When casting between QObjects, qobject_cast should always be used. This increases performance as there is no RTTI (Run Time Type Information) with qobject_cast, like there is with dynamic_cast.
Using reinterpret_cast is bad practice, as you should use it only in very specific cases.
Motivation and Context
Better code
How Has This Been Tested?
compiled and ran OBS
Types of changes
Checklist: