Bug 37673 - Programs fail to execute with a runtime error when locale is set
Summary: Programs fail to execute with a runtime error when locale is set
Status: RESOLVED DUPLICATE of bug 35353
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.2.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-09-29 11:35 UTC by Ioannis Vranos
Modified: 2008-09-30 10:21 UTC (History)
3 users (show)

See Also:
Host: Ubuntu 8.04 x86
Target: Ubuntu 8.04 x86
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
The produced .ii file from code (2.) (91.23 KB, text/plain)
2008-09-29 11:37 UTC, Ioannis Vranos
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ioannis Vranos 2008-09-29 11:35:32 UTC
Programs fail to execute with a runtime error when locale is set.


The following codes fail both for english and greek (haven't checked with other locales) with the run-time error: 

"terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid
Aborted".


The codes that fail:


1.

#include <iostream>
#include <locale>
#include <string>


int main()
{
    using namespace std;

    locale::global(locale("en_US"));

    wcin.imbue(locale("greek"));
    wcout.imbue(locale("greek"));

    wstring ws;

    wcin>> ws;

    wcout<< ws<< endl;
} 


2.

#include <iostream>
#include <locale>
#include <string>


int main()
{
    using namespace std;

    ios_base::sync_with_stdio(false);

    wcin.imbue(locale("greek"));
    wcout.imbue(locale("greek"));

    wstring ws;

    wcin>> ws;

    wcout<< ws<< endl;
} 



3.

#include <iostream>
#include <locale>
#include <string>


int main()
{
    using namespace std;



    wcin.imbue(locale("greek"));
    wcout.imbue(locale("greek"));

    wstring ws;

    wcin>> ws;

    wcout<< ws<< endl;
} 



It fails for files too:

4.

#include <locale>
#include <string>
#include <fstream>


int main()
{
    using namespace std;

    wstring ws= L"Test";

    wofstream file("filename.txt");

    file.imbue(locale("greek"));

   if(file.is_open())
      file<< ws;

}


The bug is serious, I can't save unicode texts in my programs!
Comment 1 Ioannis Vranos 2008-09-29 11:37:10 UTC
Created attachment 16424 [details]
The produced .ii file from code (2.)

The produced .ii file from code (2.) compiled with the options:

g++ -ansi -pedantic-errors -Wall -save-temps temp.cpp -o temp
Comment 2 Paolo Carlini 2008-09-29 11:46:04 UTC
Target? Named locales are supported *only* on GNU/Linux systems.
Comment 3 Paolo Carlini 2008-09-29 12:48:41 UTC

*** This bug has been marked as a duplicate of 32254 ***
Comment 4 Ioannis Vranos 2008-09-29 13:27:07 UTC
(In reply to comment #2)
> Target? Named locales are supported *only* on GNU/Linux systems.
 

Ubuntu 8.04 x86.

I am learning the QT package, and for example its QString provides a  toStdString() function that returns a std::string and a toStdWString() function that returns a std::wstring.


The specific member function I have the problem:


void MainWindow::saveFile()
{
    using namespace std;
    
    
    QString fileName= QFileDialog::getSaveFileName(this, tr("Save file"), tr(""), tr("Text files (*.txt)"));
    
    
    if(!fileName.isEmpty())
    {
        string cFileName= fileName.toStdString();
        
        wofstream cFile(cFileName.c_str());
        
                 
        if(cFile.is_open())
        {
            QString text= textEdit->toPlainText();
            
            wstring writtenText= text.toStdWString();
            
            cFile<< writtenText;
         }
      
         
         else
            QMessageBox::critical(this, "Unable to save file!", "The file could not be created!", QMessageBox::Close);
      }
}


My system *is* GNU/Linux. Apart from that, why "named locales are supported only on GNU/Linux systems"?


MINGW doesn't accept them for example?


In any case, my problem is in a GNU/Linux system.
Comment 5 Paolo Carlini 2008-09-29 13:37:39 UTC
Given the problem you are reporting, the issue is definitely that either the GNU locale model has not been selected at build time, or the localedata is not available, please refer to 32254, for example, or many similar PRs of this type. Otherwise, the type of snippet which you are reporting about is daily tested multiple times by all the developers, many variants in the testsuite. As regards the generic locale model, we would not be able to guarantee multithread safety, but improvements are in principle welcome, just send over patches. Thanks.

*** This bug has been marked as a duplicate of 32254 ***

*** This bug has been marked as a duplicate of 32254 ***
Comment 6 Ioannis Vranos 2008-09-29 13:55:14 UTC
The bug reports you are mentioning combined with 35353 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35353), means that the locales implementation of GCC is a mess.

I think the proper solution is to fix the locale problem, not trying to ignore it.
Comment 7 Paolo Carlini 2008-09-29 14:17:36 UTC
Thanks for your nice, encouraging words. We don't need duplicate reports, thanks.

*** This bug has been marked as a duplicate of 32254 ***
Comment 8 Ioannis Vranos 2008-09-30 09:51:54 UTC
Note: Change the encoding of your browser and email client to Greek-ISO or Unicode, so as to see the messages correctly.



locale -a displays the following:

john@john-desktop:~/Desktop/download/extract$ locale -a
C
el_CY.utf8
el_GR.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZW.utf8
POSIX
john@john-desktop:~/Desktop/download/extract$ 


==> Using these locales in the 3 of the 4 codes supplied, they work OK.


However the code number (3.) works only for "en_US.utf8", and it doesn't work for "el_GR.utf8".


For "en_US.utf8":

#include <iostream>
#include <locale>
#include <string>


int main()
{
    using namespace std;

    wcin.imbue(locale("en_US.utf8"));
    wcout.imbue(locale("en_US.utf8"));

    wstring ws;

    wcin>> ws;

    wcout<< ws<< endl;
} 


john@john-desktop:~/Desktop/download/extract$ ./main
Test
Test
john@john-desktop:~/Desktop/download/extract$ 


For: "el_GR.utf8"

#include <iostream>
#include <locale>
#include <string>


int main()
{
    using namespace std;

    wcin.imbue(locale("el_GR.utf8"));
    wcout.imbue(locale("el_GR.utf8"));

    wstring ws;

    wcin>> ws;

    wcout<< ws<< endl;
} 


john@john-desktop:~/Desktop/download/extract$ ./main
&#916;&#959;&#954;&#953;&#956;&#945;&#963;&#964;&#953;&#954;&#972;

john@john-desktop:~/Desktop/download/extract$



As I said, the locales work in the 3 of the 4 codes supplied, but code number (3.) doesn't.

So this isn't a "blocker" bug, but there is still a bug with wcin.imbue() and wcout.imbue() when they are used alone without a 

"locale::global(locale("en_US"));"

or 

"ios_base::sync_with_stdio(false);"


statement.
 

wofstream works OK without any of the above statements.

For example the code:


#include <locale>
#include <string>
#include <fstream>


int main()
{
    using namespace std;

    wstring ws= L"&#916;&#959;&#954;&#953;&#956;&#945;&#963;&#964;&#953;&#954;&#972;\n";

    wofstream file("filename.txt");

    file.imbue(locale("el_GR.utf8"));

   if(file.is_open())
      file<< ws;

}

works OK:

john@john-desktop:~/Desktop/download/extract$ g++ -ansi -pedantic-errors -Wall main.cpp -o main
john@john-desktop:~/Desktop/download/extract$ cat filename.txt 
&#916;&#959;&#954;&#953;&#956;&#945;&#963;&#964;&#953;&#954;&#972;
john@john-desktop:~/Desktop/download/extract$ 
Comment 9 Paolo Carlini 2008-09-30 10:21:55 UTC
(In reply to comment #8)
> So this isn't a "blocker" bug, but there is still a bug with wcin.imbue() and
> wcout.imbue() when they are used alone without a 
> 
> "locale::global(locale("en_US"));"
> 
> or 
> 
> "ios_base::sync_with_stdio(false);"

and this is clearly a duplicate of libstdc++/35353, which, note, given the current wording of the standard, is a Quality of Implementation Issue, not a proper bug. Anyway, as I said, nothing new here.


*** This bug has been marked as a duplicate of 35353 ***