static const in .h file

Marc
Posts: 6
Joined: Wed Jul 15, 2009 11:39 pm

static const in .h file

Post by Marc »

Hi !

I wondered about btQuaternion.h line 289: There it says:

Code: Select all

	static const btQuaternion&	getIdentity()
	{
		static const btQuaternion identityQuat(btScalar(0.),btScalar(0.),btScalar(0.),btScalar(1.));
		return identityQuat;
	}
I'm not entirely sure, but doesn't that create an Identity-Quaternion for every .cpp file including this header file? Maybe it would be better to replace it with

Code: Select all

	static const btQuaternion&	getIdentity()
	{
		extern btQuaternion identityQuat;
		return identityQuat;
	}
and put

Code: Select all

         btQuaternion identityQuat (btScalar(0.),btScalar(0.),btScalar(0.),btScalar(1.));
in some .cpp file?

For me, this is especially creating difficulties because my project is has some files with c++/clr support turned on and having statically created variables that have to be constructed in a header file included by .cpp file with and without clr support turned on creates some kind of race condition at the beginning of the program when all static variables get created.

I checked libbulletcollison, libbulletdynamics and libbulletmath and that seems to be the only place of this kind. I'm not 100% sure my reason is correct, but changing this line of code like above seems to solve my problem (20 successful starts in a row while otherwise it crashes imediately at elast once every 3 runs).
User avatar
ejtttje
Posts: 96
Joined: Mon Nov 03, 2008 9:57 pm

Re: static const in .h file

Post by ejtttje »

Hmm, this is a strange solution because I would expect the opposite to happen.

The function wrapper (getIdentity) around the static variable (identityQuat) causes its initialized to be delayed until the function is first called. This pattern enforces dependencies to initialized things in the required order. The first oddity is that it shouldn't matter in terms of runtime stability whether there are separate identityQuat's per translation unit (aka per .cpp file) — each would be initialized as needed by code in that translation unit.

But also I've not seen an extern *within* a function like that before. Would this break the purpose of using a function wrapper for the static? In other words, would the extern be initialized directly by the static initialization phase, and not when the function is called? If that were the case, then it would be possible for something else in the static initialization to call getIdentity() and still get an uninitialized identityQuat depending on the arbitrary static initialization phase order.

I see getIdentity's pattern used a lot (e.g. "item 47" of Scott Meyer's Effective C++), so it's interesting if it doesn't work in some environments. A workaround may be to move the function implementation to a separate .cpp file?
User avatar
ejtttje
Posts: 96
Joined: Mon Nov 03, 2008 9:57 pm

Re: static const in .h file

Post by ejtttje »

Marc
Posts: 6
Joined: Wed Jul 15, 2009 11:39 pm

Re: static const in .h file

Post by Marc »

Thx. Interesting, I didn't know that the construction of those local static variables get delayed.

Anyways, I got this problem, and I got it before in my own code at similar places as well and was able to solve it like that, if I don't do it, I sometimes (that's why I assumed it's a race condition) get an exception like this before my main function gets reached:

Code: Select all

Unhandled Exception: System.TypeInitializationException: The type initializer fo
r '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadExcep
tion: The C++ module failed to load during native initialization.
 ---> System.AccessViolationException: Attempted to read or write protected memo
ry. This is often an indication that other memory is corrupt.
   at _initterm((fnptr)* pfbegin, (fnptr)* pfend) in f:\dd\vctools\crt_bld\self_
x86\crt\src\puremsilcode.cpp:line 129
   at <CrtImplementationDetails>.LanguageSupport.InitializeNative(LanguageSuppor
t* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 554
   at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 677
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) i
n f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 875
   --- End of inner exception stack trace ---
   at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, E
xception innerException)
   at <CrtImplementationDetails>.ThrowModuleLoadException(String , Exception )
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) i
n f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 885
   at .cctor() in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 922
   --- End of inner exception stack trace ---
User avatar
ejtttje
Posts: 96
Joined: Mon Nov 03, 2008 9:57 pm

Re: static const in .h file

Post by ejtttje »

Marc wrote:was able to solve it like that, if I don't do it, I sometimes (that's why I assumed it's a race condition) get an exception like this before my main function gets reached:
I'd suggest taking it up with a microsoft support forum, unless anyone in the peanut gallery has seen anything like this.
Even if it is a Microsoft bug, you could try moving the getIdentity function implementation into a .cpp file somewhere and see if that works for you without affecting other platforms as well. (although it would kill inlining :() It looks like the entire stack trace is system libraries, so it doesn't even sound like an initialization order issue (otherwise you'd see one of your functions in the trace as it's accessing some uninitialized item...)

BTW, static initialization is single threaded, so "race condition" doesn't quite apply. It's a similar effect due to the arbitrary initialization order, but saying that instead of "initialization order" might throw some people off. (unless you actually are doing some kind of dynamic module loading at runtime where your application has already spawned threads..)
Marc
Posts: 6
Joined: Wed Jul 15, 2009 11:39 pm

Re: static const in .h file

Post by Marc »

I thought the same and posted the question here http://social.msdn.microsoft.com/Forums ... 3c177a2636. I know i theory, static initialization has to be single threaded, but since it's mixed with managed code, even a single threaded application has some helper threads doing something - when looking at the task manager, it seems like there always have to be at least 4 threads ...

Shouldn't this

Code: Select all

static btRigidBody s_fixed(0, 0,0);
(located in byTypedConstraint.cpp line 20) be replaced by a singleton in a similar way then?
User avatar
ejtttje
Posts: 96
Joined: Mon Nov 03, 2008 9:57 pm

Re: static const in .h file

Post by ejtttje »

Marc wrote:Shouldn't this

Code: Select all

static btRigidBody s_fixed(0, 0,0);
(located in byTypedConstraint.cpp line 20) be replaced by a singleton in a similar way then?
Yeah, that would probably be wise. I'm kind of a noob here myself so I don't know how extensive that would be. I'd suggest adding a bug entry for it here:
http://code.google.com/p/bullet/issues/list
Marc
Posts: 6
Joined: Wed Jul 15, 2009 11:39 pm

Re: static const in .h file

Post by Marc »

Apart from this static btRigidBody regarding my crashing issue; at the microsoft forum http://social.msdn.microsoft.com/Forums ... 3c177a2636 I got the solution. In short: Somehow a wizard in vs sets the entry function wrong. Removing /entry:main from the linker solves the issue :) Thx for helping!
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York

Re: static const in .h file

Post by sparkprime »

Modern compilers will no longer need this trick due to inlining and constant folding.
jacekPUT
Posts: 1
Joined: Sun Oct 25, 2009 2:43 pm

Re: static const in .h file

Post by jacekPUT »

hi,

i built Bullet libs, linked to my project and i've already experienced 2 problems concerning static variables in bullet source files:
1) static btClock gProfileClock; at line 21 in file btQuickprof.cpp was given memory space without constructor being called - that resulted in mClockFrequency filed not set and then led to some division by 0 - at least te program was crashing and the malfunction was easy to notice

2) static btRigidBody s_fixed(0, 0,0); as mentioned above was created withou constructor being called, and some transformMatrix was zero'ed instead of being an identity matrix - which led to some more difficult to spot problems

i did some workaround for problem #1, then problem #2 appeared and i decided not to modify the lib any more and to ask you, how to link/use the library to get all static variables properly constructed: may by i should add the lib project to my project and set my project as depending on the bullet lib project? (i use MS VS 2008) how doed it happen that examples are running well and lib used in my project is not due to mentioned problems?

thank you for help
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: static const in .h file

Post by Erwin Coumans »

We have never seen such strange error.

Can you please share a small project, including the MSVC project/solution, reproducing this problem?
Just attach the zipped project to this forum topic, or on the Bullet google code issue tracker.
Thanks,
Erwin