opencv2 setlocale clashes with wxLocale Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

opencv2 setlocale clashes with wxLocale

Post by mael15 »

Hi,
I started using opencv2 and it uses

Code: Select all

setlocale(LC_NUMERIC,"C");
for the float decimal point. Now I myself use wxLocale for the language of my program resulting in:
// As we get our decimal point separator from Win32 and not the
// CRT there is a possibility of mismatch between them and this
// can easily happen if the user code called setlocale()
// instead of using wxLocale to change the locale. And this can
// result in very strange bugs elsewhere in the code as the
// assumptions that formatted strings do use the decimal
// separator actually fail, so check for it here.
wxASSERT_MSG
(
wxString::Format("%.3f", 1.23).find(str) != wxString::npos,
"Decimal separator mismatch -- did you use setlocale()?"
"If so, use wxLocale to change the locale instead."
);
so what can I do? I can hardly make opencv use wxLocale, can I? Just remove setlocale and hope for the best?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: opencv2 setlocale clashes with wxLocale

Post by doublemax »

Code: Select all

setlocale(LC_NUMERIC,"C");
When is this called and when do you get the assert? Can you make sure that wxLocale is only initialized after openCV?

I also found some posts that indicate openCV might change the locale inside any of its functions. That would be really annoying, then you'd have to wrap each openCV call and reset the locale afterwards.

Or, if you're on Windows only and use a relatively new version of VS, you could move all openCV stuff into a separate thread and use _configthreadlocale() to make locale settings not global but unique per thread.

https://docs.microsoft.com/en-us/cpp/c- ... ew=vs-2019
Use the source, Luke!
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: opencv2 setlocale clashes with wxLocale

Post by mael15 »

doublemax wrote: Fri Dec 06, 2019 4:59 pm

Code: Select all

setlocale(LC_NUMERIC,"C");
When is this called and when do you get the assert? Can you make sure that wxLocale is only initialized after openCV?
It is called when I use https://docs.opencv.org/3.4/dd/d1a/grou ... db65a25d2d to detect circles in an image and the assert appears when I close the program later. I would not know how to make sure wxLocale is initialized after opencv.
doublemax wrote: Fri Dec 06, 2019 4:59 pm I also found some posts that indicate openCV might change the locale inside any of its functions. That would be really annoying, then you'd have to wrap each openCV call and reset the locale afterwards.

Or, if you're on Windows only and use a relatively new version of VS, you could move all openCV stuff into a separate thread and use _configthreadlocale() to make locale settings not global but unique per thread.

https://docs.microsoft.com/en-us/cpp/c- ... ew=vs-2019
I actually am on Windows only with the vs 2019, so the thread option should be perfect, a bit complicated but as long as it works...
thanx! :)
Post Reply