wxWebViewにwxMemoryFSHandlerからUTF-8データを渡すと動かなくて困った件
デバッグ記録。もしかして他の人にも有用かもしれないので記載して供養する。
経緯
- wxWebViewを使ってみたくなったので、サンプル通りメモリー上のファイルシステムにHTMLファイルを登録して読みだそうとした
- wxWidgetsにはwxMemoryFSHandlerというのがあり、初期化処理をしてAddFileすると
memory:page1.htm
にアクセスすれば実ファイルを作らずに仮想的なファイルにアクセスできる、これの用途はゴミファイルを増やさずに一時ファイル的なものを増やせるというところ
wxFileSystem::AddHandler(new wxMemoryFSHandler);
wxMemoryFSHandler::AddFile("page1.htm", "<html></html>");
問題
- 日本語を含む文字列を投入して、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));
""")