LoginSignup
1
0

More than 1 year has passed since last update.

wxWebViewにwxMemoryFSHandlerからUTF-8データを渡すと動かなくて困った件

Posted at

wxWebViewにwxMemoryFSHandlerからUTF-8データを渡すと動かなくて困った件

デバッグ記録。もしかして他の人にも有用かもしれないので記載して供養する。

経緯

  • wxWebViewを使ってみたくなったので、サンプル通りメモリー上のファイルシステムにHTMLファイルを登録して読みだそうとした
  • wxWidgetsにはwxMemoryFSHandlerというのがあり、初期化処理をしてAddFileすると memory:page1.htm にアクセスすれば実ファイルを作らずに仮想的なファイルにアクセスできる、これの用途はゴミファイルを増やさずに一時ファイル的なものを増やせるというところ
wxFileSystem::AddHandler(new wxMemoryFSHandler);
wxMemoryFSHandler::AddFile("page1.htm", "<html></html>");

参考:
https://github.com/wxWidgets/wxWidgets/blob/2740737b40d30817b474e30ff76e43f0903c2e97/samples/webview/webview.cpp#L220-L239

問題

  • 日本語を含む文字列を投入して、wxWebViewに読み込ませるとエラー発生
    • バージョンはwxWidgets v3.0.5
wxFileSystem::AddHandler(new wxMemoryFSHandler);
wxMemoryFSHandler::AddFile("page1.htm", wxString("日本語文字列"));
../src/common/stream.cpp(844): assert "buf" failed in Read(): Warning: Null pointer is about to be read

streamの読み込み時にNULLポインタになってるとかなんとか、正直これではよくわからない。実験しているうちに日本語文字列だけがエラーを出すことに気づいたので、それで当たりをつけて検索した。

解決策

wxPythonの方で同様の問題があり、いったんwxScopedCharBufferに渡してからwxMemoryFSHandlerに渡している。

→ これで解決した

    # Make some more python-friendly versions of the AddFile methods accepting text or raw data
    c.find('AddFile').findOverload('textdata').ignore()
    c.addCppMethod('void', 'AddFile', '(const wxString& filename, const wxString& textdata)',
        isStatic=True,
        doc="Add a file from text data, which will first be converted to utf-8 encoded bytes.",
        body="""\
            wxScopedCharBuffer buf = textdata->utf8_str();
            wxMemoryFSHandler::AddFile(*filename, (const char*)buf, strlen(buf));
            """)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0