-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Unsafe Constant Initialization #269
Comments
Yes, I believe that The full list of static const globals on the default branch seems to be: Angle, Color, Material, Matrix3, Matrix4, Pose3, Quaternion, Vector2, Vector3, Vector4. Note that Also note that both construction and destruction order are in play here. As library code, I'd argue that ign-math should have no global constructors nor global destructors registered at all. For cross-reference, see also gazebosim/sdformat#552.
A third option (similar to the first option in the OP) would be use function-local static A fourth option (similar to the second option in the OP) is to keep the member fields static, but instead mark them constexpr at the definition. See PR #283 for one example. This is ABI-breaking but not API-breaking. Edit: This does not work on Windows vs dllexport. Therefore, I've tried option (4b) now -- use const references + constexpr storage. Therefore, my suggestion is to use:
Hopefully, there are no cases of mutable global data. |
@scpeters, @mjcarroll, and I had a discussion and our preference is to use option (4) as in #283 for the simple numeric constants and option (3) for the rest. To that end, |
(Possibly) related to this - I'm seeing a unit test failure on Fedora rawhide where it appears that the
I did a little tracing/printing, and the If I modify the initialization of |
FYI recently #290 fixed this issue (with respect to the Material map), for the |
Yes, @richmattes this has been fixed on I would suggest either attempting to use the |
Thanks, I probably should have noticed that PR in the thread above... I'm working on ign-math6 because gazebo-11 seems to be version locked to it, so I'll continue building the fedora package with my linked patch. Feel free to pull it into ign-math6 if other distros start to have issues. |
I see some new instances of potentially unsafe constant initialization in #388, #390, and #393. They almost follow the pattern of #283 but are not |
There are still some constructor+destructor fiascos:
There are still some destructor-only fiascos. Hopefully the compiler optimizes it away, but I don't think its guaranteed:
The fix for those last two is easy, in any case. Just remove the |
Thanks. I'll reopen it since we'll be bumping the major version of gz-math soon. It would be good to fix these then. |
Fixes #269. Signed-off-by: Steve Peters <scpeters@openrobotics.org>
Fixes #269. Signed-off-by: Steve Peters <scpeters@openrobotics.org>
the last concerns mentioned by @jwnimmer-tri in #269 (comment) should be fixed by #606 and #607 |
#606 has been reverted due to test failures in gz-sim |
In
ignition::math::Color
the color constants are defined likepublic: static const Color Cyan;
. While the resulting interface is nice to use, it could cause static initialization order fiasco as C++ makes no guarantee that global variables in this form is going to be initialized in usage order. In other words, another global variable could useColor::Cyan
and be initialized first.A simple fix is to convert to a function form like the following:
public: const Color& Cyan() { static auto *c = new Color(0, 1, 1, 1); return *c; }
Another solution is more involved but it could retain the ability to refer to constants without parentheses.
public: static constexpr Color Cyan = {0, 1, 1, 1};
Both solutions represent a API/ABI change, so I would like to hear some feedback first before sending out PRs.
The Vector classes might be affected by this too.
The text was updated successfully, but these errors were encountered: