Page 1 of 1

[wx3.0.1] wxTranslations::Set() crashes

Posted: Fri Mar 20, 2015 11:39 am
by Gnawer
Hello all,
I have the requirement to show dialogs and messages in adjustable language, but numbers, times, currency should always be in English locale.
So I created

Code: Select all

// globals
wxTranslations translation;

MyApp::OnInit() {
  m_Language = wxLANGUAGE_GERMAN;   // example

  locale.Init(wxLANGUAGE_ENGLISH, wxLOCALE_DONT_LOAD_DEFAULT);
  translation.AddCatalog("DI", m_Language);
  translation.SetLanguage(m_Language);
  wxTranslations::Set(&translation);
}
This works well so far. But when I close the app, I got a wxTranslations destructor error. But it's not from my wxTranslations object.
The object to be destructed seems to contain uninitialized pointers. What can I do? Thanks for any help.

Re: [wx3.0.1] wxTranslations::Set() crashes

Posted: Fri Mar 20, 2015 12:46 pm
by doublemax

Code: Select all

// globals
wxTranslations translation;
Never create any wxWidgets class as global variable, because it will be constructed and destroyed while wxWidgets is not initialized.

Re: [wx3.0.1] wxTranslations::Set() crashes

Posted: Fri Mar 20, 2015 1:34 pm
by Gnawer
OK. But I tried member variable in MyApp and local variable in MyApp::OnInit().
Always the same problem. Example:

Code: Select all

MyApp::OnInit() {
  wxTranslations trans1;
  trans1.AddCatalog("DI", wxLANGUAGE_GERMAN);
  trans1.SetLanguage(wxLANGUAGE_GERMAN);
  wxTranslations::Set(&trans1);                       // works

  wxTranslations trans2;
  trans2.AddCatalog("DI", wxLANGUAGE_ENGLISH);
  trans2.SetLanguage(wxLANGUAGE_ENGLISH);
  wxTranslations::Set(&trans2);                       // Crash !
}

Re: [wx3.0.1] wxTranslations::Set() crashes

Posted: Fri Mar 20, 2015 2:43 pm
by Gnawer
After attentive reading the wxManual I found the solution.

Code: Select all

static void wxTranslations::Set( wxTranslations *  t )
Sets current translations object. 
Deletes previous translation object and takes ownership of t. 
"Takes ownership" is the secret. So this succeeds:

Code: Select all

MyApp::OnInit() {
  wxTranslations* pTrans = new wxTranslations();
  wxTranslations::Set(pTrans);                 // "delete" is done by wxTranslations later
}