Page 1 of 1

opencv2 setlocale clashes with wxLocale

Posted: Fri Dec 06, 2019 3:51 pm
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?

Re: opencv2 setlocale clashes with wxLocale

Posted: Fri Dec 06, 2019 4:59 pm
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

Re: opencv2 setlocale clashes with wxLocale

Posted: Fri Dec 06, 2019 5:28 pm
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! :)