Segfault in simple use of codecvt<>

Bo Persson bop@gmb.dk
Sat Jan 12 13:08:00 GMT 2008


Kristian Spangsege wrote:

> The code:
>
>> #include <cwchar>
>> #include <locale>
>>
>> int main()
>> {
>>   typedef std::codecvt<wchar_t, char, std::mbstate_t> cvt_type;
>>   cvt_type const &cvt =
>>   std::use_facet<cvt_type>(std::locale("en_US")); cvt.encoding();
>>   return 0;
>> }
>
> System:
>
> gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-51)
>
> Fedora Core release 6 (Zod)
>
> Linux localhost.localdomain 2.6.20-1.2952.fc6 #1 SMP Wed May 16
> 18:59:18 EDT 2007 i686 i686 i386 GNU/Linux
>
> I specify no flags when compiling.
>
>
> The code works if I leave out "en_US":
>
>>   cvt_type const &cvt = std::use_facet<cvt_type>(std::locale());
>>   cvt.encoding();
>
>
> It also works if the passed locale is not a temporary:
>
>>   std::locale l("en_US");
>>   cvt_type const &cvt = std::use_facet<cvt_type>(l);
>>   cvt.encoding();
>
>
> And if 'encoding' is called directly on the returned facet:
>
>>   std::use_facet<cvt_type>(std::locale("en_US")).encoding();
>
>
> As far as I know, a temporary is supposed to stay "alive" until the
> end of the scope.

No. Unless it is bound to a const reference, the temporary is 
destroyed at the end of the statement, not at the end of the scope.


Bo Persson



More information about the Libstdc++ mailing list