函数宏

GObjectset_weak_pointer

since: 2.56

声明 [src]

#define g_set_weak_pointer (
  weak_pointer_location,
  new_object
)

描述 [src]

更新一个指针以弱引用 new_object

它将 new_object 赋值给 weak_pointer_location,并确保如果 new_object 被销毁,则 weak_pointer_location 将自动设置为 NULL。赋值不是原子的。弱引用不是线程安全的,详情请参见 g_object_add_weak_pointer()

weak_pointer_location 参数不能为 NULL

还包括一个允许此函数在不进行指针转换的情况下使用的宏。该函数本身是静态内联的,因此其地址可能因编译单元而异。

此函数的一个便利用途是实现属性 setter

  void
  foo_set_bar (Foo *foo,
               Bar *new_bar)
  {
    g_return_if_fail (IS_FOO (foo));
    g_return_if_fail (new_bar == NULL || IS_BAR (new_bar));

    if (g_set_weak_pointer (&foo->bar, new_bar))
      g_object_notify (foo, "bar");
  }

自 2.56 起可用

此函数不直接对语言绑定可用。

参数

weak_pointer_location

类型: -

一个指针的内存地址。

new_object

类型: -

指向要分配给它的新 GObject 的指针,或 NULL 以清除指针。