Skip to content

bug: wstring_convert to_bytes error #6360

@ivanllc

Description

@ivanllc

Duplicate Check

Describe the bug

range_error was thrown in -fno-exceptions mode with message "wstring_convert: to_bytes error"
when input in Ubuntu
Answer From Google AI:
https://share.google/aimode/RfMcAz5ELdcenXlTl

Errors during std::wstring_convert::to_bytes typically stem from invalid characters in the input wide string or configuration mismatches with the conversion facet.
Common Causes of Errors

Invalid Encoding: The most frequent cause of a std::range_error is an input string containing [unpaired surrogates](https://stackoverflow.com/questions/32182610/how-can-i-get-wstring-convertto-bytes-to-throw-a-range-error-exception) or characters that exceed the Maxcode limit of the facet.  

Deprecation: std::wstring_convert was [deprecated in C++17](http://en.cppreference.com/w/cpp/locale/wstring_convert.html) due to various usability and safety issues. Modern compilers may issue warnings or require specific flags like -std=c++11 to function.  

Facet Mismatches: If the facet provided to the constructor (e.g., std::codecvt_utf8) does not match the actual encoding of the wide string, the conversion will fail.

Endianness Issues: Using the wrong byte-order (Big-endian vs. Little-endian) can lead to crashes or garbled output during conversion.  

How to Fix or Prevent Crashes

Use Error Initializers: To prevent a std::range_error exception, you can provide a "byte-error string" to the [wstring_convert constructor](https://learn.microsoft.com/en-us/cpp/standard-library/wstring-convert-class?view=msvc-170). If conversion fails, the method returns this string instead of throwing an exception.  

Validate Source Encoding: Ensure your source files and literals are correctly encoded (e.g., using [u8 literals](https://stackoverflow.com/questions/31513578/wstring-converter-exception-when-parsing-a-c-string) like u8"text") to avoid passing invalid bytes to the converter. 

Use Modern Alternatives: Because this class is deprecated, consider using dedicated libraries like ICU (International Components for Unicode) or Boost.Nowide for more robust string handling.

Check Compiler Version: Ensure you are using a compiler with full C++11 support (e.g., GCC 4.9+), as earlier versions had buggy implementations of these facets

Code sample

Code
import flet as ft
from flet import TextField

def main(page: ft.Page):
    new_text = TextField(hint_text="Input some wstring")
    page.add(new_text)

ft.run(main)

To reproduce

When input some Chinese characters in text filed.

Expected behavior

No response

Screenshots / Videos

Captures
Screencast.from.2026-04-04.01-00-50.mp4

Operating System

Linux

Operating system details

Ubuntu 24.04.4 LTS

Flet version

0.82.2

Regression

I'm not sure / I don't know

Suggestions

No response

Logs

Logs
range_error was thrown in -fno-exceptions mode with message "wstring_convert: to_bytes error"

Additional details

Input method IBus Table 1.17.4
Also happen in Android.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions