tags:

views:

72

answers:

1
//s_request_view() constructor is declared as below
namespace Identity_VIEW
{
Published_view_identity s_request_view("SAMPLE");
};

//The constructor is called in another source file as below,
bind_view(Identity_VIEW::s_request_view);

This same code is working fine on windows but on SBC (PowerPC) s_request_view is passed as NULL.

Can anyone please help me finding out why is it behaving differently?

+6  A: 

I think, the answer here is that compiler does not guarantee the order of global variables initialization. If your bind_view is called from constructor of another global variable - you'll have a floating bug.

Try using the following approach:

namespace Identity_VIEW
{
   Published_view_identity & getRequestView()
   {
      static Published_view_identity s_request_view ("Sample");
      return s_request_view;
   }
}

...
bind_view(Identity_VIEW::getRequestView());

That can help resolving the order of the global variables initialization. Nevertheless, this solution is not thread-safe (in case you care)...

SadSido
+1 for showing a much safer, access-is-initialization kind of method and then going beyond to discuss the thread safety issues with the initialization of s_request_view.