Skip to content

Commit

Permalink
Check critical static variables are initialized
Browse files Browse the repository at this point in the history
In iOS the following innocent statement:

static Ogre::Radian globalVar =  Ogre::Degree( 90.0 );

can result in globalVar = 0 if static variables such as Math::PI are not
yet initialized.

This introduces very subtle bugs in iOS that are hard to detect and yet
work fine on other platforms
  • Loading branch information
darksylinc committed Jan 10, 2023
1 parent d1984c5 commit 9c5ba9b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
14 changes: 12 additions & 2 deletions OgreMain/include/OgreMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,18 @@ namespace Ogre
return ( !useTables ) ? std::tan( fValue ) : TanTable( fValue );
}

static inline Real DegreesToRadians( Real degrees ) { return degrees * fDeg2Rad; }
static inline Real RadiansToDegrees( Real radians ) { return radians * fRad2Deg; }
static inline Real DegreesToRadians( Real degrees )
{
OGRE_ASSERT_HIGH( fDeg2Rad != 0.0f &&
"Ogre library static variables are initialized out of order" );
return degrees * fDeg2Rad;
}
static inline Real RadiansToDegrees( Real radians )
{
OGRE_ASSERT_HIGH( fRad2Deg != 0.0f &&
"Ogre library static variables are initialized out of order" );
return radians * fRad2Deg;
}

/** These functions used to set the assumed angle units (radians or degrees)
expected when using the Angle type.
Expand Down
23 changes: 23 additions & 0 deletions OgreMain/src/OgreMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,17 @@ namespace Ogre
}
else
{
OGRE_ASSERT_HIGH( PI != 0.0f &&
"Ogre library static variables are initialized out of order" );
return Radian( PI );
}
}
//-----------------------------------------------------------------------
Radian Math::ASin( Real fValue )
{
OGRE_ASSERT_HIGH( HALF_PI != 0.0f &&
"Ogre library static variables are initialized out of order" );

if( -1.0 < fValue )
{
if( fValue < 1.0 )
Expand Down Expand Up @@ -193,7 +198,11 @@ namespace Ogre
Real Math::AngleUnitsToRadians( Real angleunits )
{
if( msAngleUnit == AU_DEGREE )
{
OGRE_ASSERT_HIGH( fDeg2Rad != 0.0f &&
"Ogre library static variables are initialized out of order" );
return angleunits * fDeg2Rad;
}
else
return angleunits;
}
Expand All @@ -202,7 +211,11 @@ namespace Ogre
Real Math::RadiansToAngleUnits( Real radians )
{
if( msAngleUnit == AU_DEGREE )
{
OGRE_ASSERT_HIGH( fRad2Deg != 0.0f &&
"Ogre library static variables are initialized out of order" );
return radians * fRad2Deg;
}
else
return radians;
}
Expand All @@ -211,7 +224,11 @@ namespace Ogre
Real Math::AngleUnitsToDegrees( Real angleunits )
{
if( msAngleUnit == AU_RADIAN )
{
OGRE_ASSERT_HIGH( fRad2Deg != 0.0f &&
"Ogre library static variables are initialized out of order" );
return angleunits * fRad2Deg;
}
else
return angleunits;
}
Expand All @@ -220,7 +237,11 @@ namespace Ogre
Real Math::DegreesToAngleUnits( Real degrees )
{
if( msAngleUnit == AU_RADIAN )
{
OGRE_ASSERT_HIGH( fDeg2Rad != 0.0f &&
"Ogre library static variables are initialized out of order" );
return degrees * fDeg2Rad;
}
else
return degrees;
}
Expand Down Expand Up @@ -955,6 +976,8 @@ namespace Ogre
//-----------------------------------------------------------------------
Real Math::gaussianDistribution( Real x, Real offset, Real scale )
{
OGRE_ASSERT_HIGH( PI != 0.0f && "Ogre library static variables are initialized out of order" );

Real nom = Math::Exp( -Math::Sqr( x - offset ) / ( 2 * Math::Sqr( scale ) ) );
Real denom = scale * Math::Sqrt( 2 * Math::PI );

Expand Down

0 comments on commit 9c5ba9b

Please sign in to comment.