在编译和运行大致以下代码时,我遇到了警告和分段错误,该代码在string
上使用to_utf32_fast
方法,应返回codepoint_count
变量中编码的 UTF-32 字符数。此方法转译为 C 函数g_utf8_to_ucs4_fast
,不知何故out codepoint_count
最终成为long * *
参数而不是预期的long *
。
我确实有一个解决方法,所以这并不紧急。
int main (string[] args) {
string test = "abc";
long codepoint_count;
string utf32_version = test.to_utf32_fast(-1, out codepoint_count).to_string();
stdout.printf("success");
return 0;
}
输出的相关部分:
C:/msys64/usr/src/outerror.vala.c: In function '_vala_main':
C:/msys64/usr/src/outerror.vala.c:78:50: warning: passing argument 3 of 'g_utf8_to_ucs4_fast' from incompatible pointer type [-Wincompatible-pointer-types]
_tmp2_ = g_utf8_to_ucs4_fast (test, (glong) -1, &_tmp1_);
^
In file included from C:/msys64/mingw64/include/glib-2.0/glib/gstring.h:33:0,
from C:/msys64/mingw64/include/glib-2.0/glib/giochannel.h:34,
from C:/msys64/mingw64/include/glib-2.0/glib.h:54,
from C:/msys64/usr/src/outerror.vala.c:5:
C:/msys64/mingw64/include/glib-2.0/glib/gunicode.h:798:12: note: expected 'glong * {aka long int *}' but argument is of type 'glong ** {aka long int **}'
gunichar * g_utf8_to_ucs4_fast (const gchar *str,
^~~~~~~~~~~~~~~~~~~
我查看了转译的 C 源代码,要g_utf8_to_ucs4_fast
的第三个参数是指向 long 的未初始化指针的地址,后来用g_free
释放。这会在程序运行时触发段错误。
我在调用此函数的方式上做错了什么吗?它被声明为public string32 to_utf32_fast (long len = -1, out long? items_written = null)
.
我是 Vala 的新手(更熟悉 C(,不确定我是否掌握了参数注释。第二个参数不应该被转换为 C 作为long **
而不是long *
,但也许可空性标记?
或默认值= null
导致 Vala 认为变量items_written
是指向long
(或其 Vala 等价物(的指针,而不是long
。如果是这样,那么方法的声明中可能存在错误或 Vala 语法中的歧义。
声明是错误的。这段代码工作得很好:
[CCode (cname = "g_utf8_to_ucs4_fast")]
public extern string32 to_utf32_fast_ (string s, long len = -1,
out long items_written);
int main (string[] args) {
string test = "abc";
long codepoint_count;
string32 utf32_version = to_utf32_fast_(test, -1, out codepoint_count);
stdout.printf("success");
return 0;
}
在 glib-2.0.vapi 的原始声明中,items_written 参数将是 C 中的glong**
,但它实际上是一个glong*
。
我已将此报告为错误:
https://gitlab.gnome.org/GNOME/vala/issues/634