title: CFLocaleCopyCurrent stale value
body: <p>At my workplace, our app determines the locale of the user session by using a code that is similar to below (though there are various layers of code it passes through before it reaches here at the time of startup, so the problem is/may not evident by running the code below)</p>
  
  <pre><code>#include &lt;CoreFoundation/CoreFoundation.h&gt;  
  #include &lt;iostream&gt;  
  #include &lt;string&gt;  
  #include &lt;vector&gt;  
  #include &lt;memory&gt;  
  
    // Reference release  
      struct reference_close  
      {  
          void operator()(const void *ref) const  
          {  
              CFRelease(static_cast&lt;CFTypeRef&gt;(ref));  
          }  
      }; // end of reference_close structure  
  
      typedef std::unique_ptr&lt;const void, reference_close&gt;  reference_uptr;  
  
      std::string get_user_locale()  
      {  
          reference_uptr  ref_ptr(CFLocaleCopyCurrent());  
          CFLocaleRef    locale_ref(static_cast&lt;CFLocaleRef&gt;(ref_ptr.get()));  
          if (locale_ref == nullptr)  
          {  
              return std::string();  
          }  
          const size_t      default_size(128);  
          std::vector&lt;char&gt;  buff(default_size);  
          CFStringRef        str_ref(CFLocaleGetIdentifier(locale_ref));  
  
        if (str_ref != nullptr)  
          {  
              CFIndex  len(CFStringGetLength(str_ref) + 1);  
              if (len &gt; boost::numeric_cast&lt;CFIndex&gt;(default_size))  
              {  
                  buff.resize(len);  
              }  
  
              buff[0] = 0;  
              if (!CFStringGetCString(str_ref, &amp;buff[0], len, kCFStringEncodingISOLatin1))  
              {  
                  return std::string();  
              }  
          }  
  
          return std::string(&amp;buff[0]);  
      } // end of get_user_locale()  
  
  int main()  
  {  
      std::cout &lt;&lt; "get_user_locale() : "&lt;&lt;get_user_locale() &lt;&lt; std::endl;  
  
      return 0;  
  }  
  </code></pre>
  
  <p>The app has a well defined bundle structure with necessary resources and localization resource directories e.g <code>Contents/Resources/ja.lproj</code></p>
  
  <p>Lately, we are facing an issue wherein we do the following</p>
  
  <p>1) Change the system language and country in the preferences e.g. change from en_US to ja_JP</p>
  
  <p>2) Reboot the machine</p>
  
  <p>3) Launch the app and see the erroneous output e.g en_JP</p>
  
  <p>4) Re-launch the app to get the correct answer as ja_JP</p>
  
  <p>I have read the documentation of <a href="https://developer.apple.com/documentation/corefoundation/1542508-cflocalecopycurrent" rel="nofollow noreferrer">CFLocaleCopyCurrent</a> which states that</p>
  
  <blockquote>
    <p>Settings you get from this locale do not change as a user's
    preferences are changed so that your operations are consistent.
    Typically you perform some operations on the returned object and then
    release it. Since the returned object may be cached, you do not need
    to hold on to it indefinitely.</p>
  </blockquote>
  
  <p>I also checked the another documentation on <a href="https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFLocales/Articles/CFLocaleWorking.html#//apple_ref/doc/uid/20002241-164844" rel="nofollow noreferrer">lifetime of CFLocale</a> which states that </p>
  
  <blockquote>
    <p>The object you get back from CFLocaleCopyCurrent does not change when
    the user changes their Preferences settings. Moreover, the object
    itself may be cached by the runtime system, so successive calls of
    CFLocaleCopyCurrent may return the same object, even if a user has
    changed preference settings. If you want to ensure that your locale
    settings are consistent with user preferences, you must synchronize
    preferences and get a new locale object with CFLocaleCopyCurrent.</p>
  </blockquote>
  
  <p>Given this info, I tried to incorporate <code>CFPreferencesSynchronize</code> and/or <code>CFPreferencesSynchronize</code> in the app code (just before I call the <code>CFLocaleCopyCurrent</code>) to get the most updated locale. But no luck.</p>
  
  <p>Do I need to do anything extra to make sure that I get the most recent/updated value of locale object from the system preferences ?</p>