Vala 函数调用中使用的拥有属性创建 "black magic C code"
Owned attribute used in Vala function call creates "black magic C code"
这是 Vala 代码中的示例。
有一个全局的 Glib.Llist vstack 存储了一些指针。
List<void*> vstack;
有一个 change_state 函数将第二个参数存储在全局 vstack 列表中。
void change_state( enum..., void*, enum... );
此 goto_info 测试函数(调用 change_state)不起作用,因为当它returns,指针被释放,即使它存储在列表中
// Vala source
void goto_info()
{
StInfo sti = new StInfo();
change_state( SUPS.INFO_PAGE, sti, StackDir.FORW );
}
// C output
void goto_info (void) {
StInfo* sti = NULL;
StInfo* _tmp0_ = NULL;
_tmp0_ = st_info_new ();
sti = _tmp0_;
change_state (SUPS_INFO_PAGE, sti, STACK_DIR_FORW);
_g_object_unref0 (sti);
}
但是如果我只添加 owned 属性,代码就可以工作(指针在 goto_info函数),但我实在看不懂C输出有什么变化。
// Vala source
void goto_info()
{
StInfo sti = new StInfo();
change_state( SUPS.INFO_PAGE, ( owned )sti, StackDir.FORW );
}
// C output
void goto_info (void) {
StInfo* sti = NULL;
StInfo* _tmp0_ = NULL;
StInfo* _tmp1_ = NULL;
_tmp0_ = st_info_new ();
sti = _tmp0_;
_tmp1_ = sti;
sti = NULL;
change_state (SUPS_INFO_PAGE, _tmp1_, STACK_DIR_FORW);
_g_object_unref0 (sti);
}
重要提示: goto_info 是唯一在我添加 owned 时发生变化的函数,Vala 不会触及其他任何函数编译器。有人可以解释这个 C 输出代码中隐藏的黑魔法在哪里?如果对指针的 ref/unref 调用相同,为什么第二个版本有效?
注意这一行:
sti = NULL;
sti
设置为 NULL
(而 _tmp1_
被传递给 change_state
),因此现在对 unref 的调用是一个空操作:
_g_object_unref0 (sti);
这是 Vala 代码中的示例。
有一个全局的 Glib.Llist vstack 存储了一些指针。
List<void*> vstack;
有一个 change_state 函数将第二个参数存储在全局 vstack 列表中。
void change_state( enum..., void*, enum... );
此 goto_info 测试函数(调用 change_state)不起作用,因为当它returns,指针被释放,即使它存储在列表中
// Vala source
void goto_info()
{
StInfo sti = new StInfo();
change_state( SUPS.INFO_PAGE, sti, StackDir.FORW );
}
// C output
void goto_info (void) {
StInfo* sti = NULL;
StInfo* _tmp0_ = NULL;
_tmp0_ = st_info_new ();
sti = _tmp0_;
change_state (SUPS_INFO_PAGE, sti, STACK_DIR_FORW);
_g_object_unref0 (sti);
}
但是如果我只添加 owned 属性,代码就可以工作(指针在 goto_info函数),但我实在看不懂C输出有什么变化。
// Vala source
void goto_info()
{
StInfo sti = new StInfo();
change_state( SUPS.INFO_PAGE, ( owned )sti, StackDir.FORW );
}
// C output
void goto_info (void) {
StInfo* sti = NULL;
StInfo* _tmp0_ = NULL;
StInfo* _tmp1_ = NULL;
_tmp0_ = st_info_new ();
sti = _tmp0_;
_tmp1_ = sti;
sti = NULL;
change_state (SUPS_INFO_PAGE, _tmp1_, STACK_DIR_FORW);
_g_object_unref0 (sti);
}
重要提示: goto_info 是唯一在我添加 owned 时发生变化的函数,Vala 不会触及其他任何函数编译器。有人可以解释这个 C 输出代码中隐藏的黑魔法在哪里?如果对指针的 ref/unref 调用相同,为什么第二个版本有效?
注意这一行:
sti = NULL;
sti
设置为 NULL
(而 _tmp1_
被传递给 change_state
),因此现在对 unref 的调用是一个空操作:
_g_object_unref0 (sti);