以下来自对 cocos2d-x-3.5 的源码分析,今天重点研究它的动态开辟的对象释放问题.
涉及以下3个基本的class
和一个模板类
手动引用计数类
class Ref
Ref
是 cocos2d 中很大一部分类的祖先类 . @H_404_19@Ref
支持以下三个接口:
retain()
累加引用计数,当我们将指针复制给新的指针的时候,请自觉手动调用! @H_404_19@release()
指针释放无用或者即将挪作它用之前,自己自觉手动调用!@H_404_19@autorelease()
将释放的任务交给当前的AutoreleasePool
,这样程序员就不需要在意这个指针什么时候被释放或者挪作它用了. @H_404_19@
Ref
对象池AutoreleasePool
- 任何一个
AutoreleasePool
对象构造的时候就会被压栈到下面的PoolManager
中,析构的时候自动退栈 .@H_404_19@ AutoreleasePool
会在clear
或者析构的时候将自己管理的Ref
自动release()
一下. @H_404_19@AutoreleasePool
支持:
- 添加
void addObject(Ref *object)
@H_404_19@ - 查询
bool contains(Ref* object) const
@H_404_19@ - 清除
void clear()
@H_404_19@
- 添加
- 任何一个
对象池管理员
PoolManager
PoolManager
是单例的 !
static PoolManager* getInstance()
@H_404_19@
- 池子只能有压栈和退栈两个操作.
@H_404_19@ - 构造单例时候会自动构造一个
AutoreleasePool
作为当前使用的池子.@H_404_19@ - 总是使用栈顶的池子.@H_404_19@
模板类
@H_404_19@RefPtr
模拟shared_ptr
另外还有一个相关的class : class Clonable
.
* 支持接口 Clonable* clone()
,会复制Ref
,重新开始计数.
可以看到 cocos2d-x 提供了基本的管理机制,但是需要依赖程序员的自觉性!!!
基于Ref的继承体系速查
Ref
Director
DisplayLinkDirector
@H_404_19@
EventListener
EventListenerAcceleration
@H_404_19@EventListenerController
@H_404_19@EventListenerCustom
@H_404_19@EventListenerFocus
@H_404_19@EventListenerKeyboard
@H_404_19@EventListenerMouse
@H_404_19@EventListenerTouchOneByOne
@H_404_19@
Event
EventDispatcher
@H_404_19@EventController
@H_404_19@EventFocus
@H_404_19@EventCustom
@H_404_19@EventKeyboard
@H_404_19@EventMouse
@H_404_19@EventTouch
@H_404_19@
Scheduler
@H_404_19@ScriptHandlerEntry
@H_404_19@Touch
@H_404_19@ActionManager
@H_404_19@Action
:Cloneable
FiniteTimeAction
ActionInstant
StopGrid
@H_404_19@
ActionInterval
GridAction
@H_404_19@AccelAmplitude
@H_404_19@DeccelAmplitude
@H_404_19@ActionEase
EaseBackIn
@H_404_19@EaseBackInOut
@H_404_19@EaseBackOut
@H_404_19@EaseBezierAction
@H_404_19@EaseBounce
@H_404_19@EaseCircleActionIn
@H_404_19@EaseCircleActionInOut
@H_404_19@EaseCircleActionOut
@H_404_19@EaseCubicActionIn
@H_404_19@EaseCubicActionInOut
@H_404_19@
Animate
@H_404_19@BezierBy
@H_404_19@BezierTo
@H_404_19@Blink
@H_404_19@FadeTo
@H_404_19@JumpBy
JumpTo
@H_404_19@
MoveBy
MoveTo
@H_404_19@
Repeat
@H_404_19@RepeatForever
@H_404_19@ReverseTime
@H_404_19@RotateBy
@H_404_19@RotateTo
@H_404_19@ScaleTo
ScaleBy
@H_404_19@
TintBy
@H_404_19@TintTo
@H_404_19@TargetedAction
@H_404_19@ProgressFromTo
@H_404_19@ProgressTo
@H_404_19@ActionTween
@H_404_19@
Sequence
@H_404_19@SkewTo
SkewBy
@H_404_19@
Spawn
@H_404_19@
Follow
@H_404_19@Speed
@H_404_19@ActionCamera
OrbitCamera
@H_404_19@
PointArray
@H_404_19@AnimationFrame : Cloneable
@H_404_19@Animation : Cloneable
@H_404_19@AnimationCache
@H_404_19@Node
Camera
@H_404_19@BaseLight
@H_404_19@AtlasNode
@H_404_19@ClippingNode
@H_404_19@ClippingRectangleNode
@H_404_19@DrawNode
@H_404_19@TMXLayer
@H_404_19@TMXTiledMap
@H_404_19@LabelTTF
:LabelProtocol
,BlendProtocol
@H_404_19@LabelBMFont
:LabelProtocol
,BlendProtocol
@H_404_19@Layer
LayerMultiplex
@H_404_19@LayerColor
:BlendProtocol
LayerGradient
@H_404_19@
Menu
@H_404_19@MenuItem
@H_404_19@ImageView
@H_404_19@
MotionStreak
:TextureProtocol
@H_404_19@NodeGrid
@H_404_19@RenderTexture
@H_404_19@ParallaxNode
@H_404_19@ProtectedNode
Widget
:LayoutParameterProtocol
Button
@H_404_19@CheckBox
@H_404_19@LoadingBar
@H_404_19@Slider
@H_404_19@Text
@H_404_19@TextAtlas
@H_404_19@TextBMFont
@H_404_19@VideoPlayer
@H_404_19@WebView
@H_404_19@Layout
:LayoutProtocol
RelativeBox
@H_404_19@PageView
@H_404_19@HBox
@H_404_19@VBox
@H_404_19@ScrollView
ListView
@H_404_19@
ParticleBatchNode
:TextureProtocol
@H_404_19@ParticleSystem
:TextureProtocol
ParticleSystemQuad
ParticleExplosion
@H_404_19@ParticleMeteor
@H_404_19@ParticleFire
@H_404_19@ParticleFireworks
@H_404_19@ParticleFlower
@H_404_19@ParticleGalaxy
@H_404_19@ParticleRain
@H_404_19@ParticleSmoke
@H_404_19@ParticleSnow
@H_404_19@ParticleSpiral
@H_404_19@ParticleSun
@H_404_19@
Scene
TransitionScene
TransitionCrossFade
@H_404_19@TransitionFade
@H_404_19@TransitionJumpZoom
@H_404_19@TransitionShrinkGrow
:TransitionEaseScene
@H_404_19@TransitionMoveInL
:TransitionEaseScene
@H_404_19@TransitionSceneOriented
TransitionFlipAngular
@H_404_19@TransitionFlipX
@H_404_19@TransitionFlipY
@H_404_19@TransitionZoomFlipX
@H_404_19@TransitionZoomFlipY
@H_404_19@
TransitionFadeTR
:TransitionEaseScene
@H_404_19@TransitionSlideInL
:TransitionEaseScene
@H_404_19@TransitionSplitCols
:TransitionEaseScene
TransitionSplitRows
@H_404_19@
TransitionTurnOffTiles
@H_404_19@TransitionPageTurn
@H_404_19@TransitionProgress
@H_404_19@
Sprite
:TextureProtocol
@H_404_19@SpriteBatchNode
:TextureProtocol
TMXLayer
@H_404_19@
TMXTiledMap
@H_404_19@
Component
@H_404_19@FontAtlas
@H_404_19@Grabber
@H_404_19@GridBase
@H_404_19@SpriteFrame
:Clonable
@H_404_19@SpriteFrameCache
@H_404_19@TMXObjectGroup
@H_404_19@TMXLayerInfo
@H_404_19@LayoutManager
@H_404_19@RichElement
@H_404_19@GLView
@H_404_19@