[wx3.0.1] wxTranslations::Set() crashes 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
Gnawer
Experienced Solver
Experienced Solver
Posts: 54
Joined: Thu Jun 29, 2006 11:10 am
Location: Ahlen, Germany

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

Post by Gnawer » Fri Mar 20, 2015 11:39 am

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.
Best regards
Gnawer

User avatar
doublemax
Moderator
Moderator
Posts: 14098
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax » Fri Mar 20, 2015 12:46 pm

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.
Use the source, Luke!

Gnawer
Experienced Solver
Experienced Solver
Posts: 54
Joined: Thu Jun 29, 2006 11:10 am
Location: Ahlen, Germany

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

Post by Gnawer » Fri Mar 20, 2015 1:34 pm

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 !
}
Best regards
Gnawer

Gnawer
Experienced Solver
Experienced Solver
Posts: 54
Joined: Thu Jun 29, 2006 11:10 am
Location: Ahlen, Germany

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

Post by Gnawer » Fri Mar 20, 2015 2:43 pm

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
}
Best regards
Gnawer

Post Reply