我们是否应该始终使用g_object_ref_sink而不是g_object_ref? [英] Should we always use g_object_ref_sink instead of g_object_ref?

查看:156
本文介绍了我们是否应该始终使用g_object_ref_sink而不是g_object_ref?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GLib中有两个函数可用于GObject对象的引用计数:

There are two functions in GLib that work with the reference counting of the GObject objects:

  • g_object_ref 增加对象的引用计数,不处理浮动引用.
  • g_object_ref_sink 增加对象的引用计数,或者如果引用是浮动的,则从对象中删除 floating 标志.
  • g_object_ref increases reference count of the object, doesn't handle floating references.
  • g_object_ref_sink increases reference count of the object or just removes floating flag from the object if the reference is floating.

由于我们不知道对象是否在浮动,我们应该始终使用 g_object_ref_sink ,不是吗?如果我错了,什么时候应该使用 g_object_ref ,什么时候应该使用 g_object_ref_sink ?我们应该只对 GInitiallyUnowned 对象使用 g_object_ref_sink 吗?

Since we don't know if the object is floating or not, we should always use g_object_ref_sink, shouldn't we? If I am wrong, when should we use g_object_ref and when should we use g_object_ref_sink? Should we use g_object_ref_sink only for GInitiallyUnowned objects?

推荐答案

您通常应该知道要处理的对象的类型(即,指针通常比 GObject * 具有更具体的类型),这样您就可以知道它是否可能在浮动.使用浮动引用的基于GObject的API被记录为正在使用它们.没有记录为使用浮动引用的任何内容都不会.

You should generally know the type of an object you’re handling (i.e. pointers typically have more specific types than GObject*), so you’ll know if it’s potentially floating or not. GObject-based APIs which use floating references are documented as using them. Anything which isn’t documented as using floating references, doesn’t.

这篇关于我们是否应该始终使用g_object_ref_sink而不是g_object_ref?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆