GObject内存管理模型

本文来自于GObject Reference Manual.

GObject的内存管理想法上很简单,但API稍显复杂:目的是提供一个基于引用计数(Reference Counting)可用于不同内存管理模型(如,GC)的灵活模型。

函数g_object_ref/g_object_unref用于对一个GObject增加或减少引用计数。引用计数由g_object_new赋初值,即当前调用者为仅有的GObject对象的拥有者。一旦引用计数为0,,即g_object_unref由最后一个引用此对象的使用者调用后,类的dispose和finalize方法则会被激活。如果被释放的对象是该类的最后一个对象,则为此类型所分配的内存,也会被释放。

GObject对象析构的时序:

如果一个对象的最后一个引用被释放,首先,dispose方法会被触发。一旦dispose执行完成,当前Object应该将内部所有资源释放,包括对其它GObject对象的引用。但此时,此对象应该在finalize方法触发之前仍能响应其它使用者的方法调用,只是要注意安全性。另外,dispose方法有可能被多次触发,这种情况发生在对象间的循环引用上,如A引用B,B又引用A,此时如果A释放B,B又可能再次释放A。dispose方法的设计应该是链式结构,在此方法中,应该调用父对象的dispose方法。之后,dispose方法执行完成后,finalize方法会被触发,在此将完成对象的析构。finalize方法只会执行一次,但同样finalize方法内部也要调用父对象的finalize方法。

对于为什么GObject对象的析构要分为俩个阶段,James Henstridge给出了解释:

对于可能发生的循环释放的问题,如上面提到的A->B, B->A,可以很容易的检测出发生了循环释放,然后通过直接触发g_object_run_dispose,可以打破循环释放!

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Table of Contents Introduction I. Concepts Background Data types and programming Exporting a C API The Glib Dynamic Type System Copy functions Conventions Non-Instantiable non-classed fundamental types Instantiable classed types: objects Initialization and Destruction Non-instantiable classed types: Interfaces. Interface Initialization Interface Destruction The GObject base class Object instanciation Object memory management Reference count Weak References Reference counts and cycles Object properties Accessing multiple properties at once The GObject messaging system Closures C Closures non-C closures (for the fearless). Signals Signal registration Signal connection Signal emission The detail argument II. API Reference GType - The GLib Runtime type identification and management system GTypePlugin - An interface for dynamically loadable types GTypeModule - Type loading modules GObject - The base object type Enums and Flags - Enumeration and flags types GBoxed - A mechanism to wrap opaque C structures registered by the type system Generic Values - A polymorphic type that can hold values of any other type Parameters and Values - Standard Parameter and Value Types Varargs Value Collection - Converting varargs to generic values GParamSpec - Metadata for parameter specifications Signals - A means for customization of object behaviour and a general purpose notification mechanism Closures - Functions as first-class objects Value arrays - A container structure to maintain an array of generic values III. Tools Reference glib-mkenums - C language enum description generation utility glib-genmarshal - C code marshaller generation utility for GLib closures gobject-query - display a tree of types IV. Tutorial How To define and implement a new GObject? Boilerplate header code Boilerplate code Object Construction Object Destruction Object methods Non-virtual public methods Virtual public methods Virtual private Methods Chaini
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值