cocos2d-x-3.5 中new 出来的对象大部分是如何处理的

以下来自对 cocos2d-x-3.5 的源码分析, 今天重点研究它的动态开辟的对象释放问题.


涉及以下3个基本的class 和一个模板类


  • 手动引用计数类 class Ref

    • Ref 是 cocos2d 中很大一部分类的祖先类 .
    • Ref 支持以下三个接口:
      • retain() 累加引用计数 , 当我们将指针复制给新的指针的时候, 请自觉手动调用!
      • release() 指针释放无用或者即将挪作它用之前,自己自觉手动调用!
      • autorelease() 将释放的任务交给当前的 AutoreleasePool , 这样程序员就不需要在意这个指针什么时候被释放或者挪作它用了.
  • Ref 对象池 AutoreleasePool

    • 任何一个AutoreleasePool 对象构造的时候就会被压栈到下面的PoolManager 中, 析构的时候自动退栈 .
    • AutoreleasePool 会在 clear 或者析构的时候将自己管理的Ref 自动release() 一下.
    • AutoreleasePool 支持:
      • 添加void addObject(Ref *object)
      • 查询bool contains(Ref* object) const
      • 清除 void clear()
  • 对象池管理员PoolManager

    • PoolManager 是单例的 !
      • static PoolManager* getInstance()
    • 池子只能有压栈和退栈两个操作.
      • void push(AutoreleasePool *pool);
      • void pop();
    • 构造单例时候会自动构造一个 AutoreleasePool 作为当前使用的池子.
    • 总是使用栈顶的池子.
  • 模板类RefPtr 模拟shared_ptr

    • 支持swap()
    • 自行测试NULL
    • 自动累加/累减计数
    • 可以相互赋值
    • 支持
      • get() 接口获取原始指针
      • * , -> 运算符

另外还有一个相关的class : class Clonable .
* 支持接口 Clonable* clone() , 会复制Ref , 重新开始计数.



可以看到 cocos2d-x 提供了基本的管理机制 ,但是需要依赖程序员的自觉性!!!


基于Ref的继承体系速查

  • Ref

    • Director
      • DisplayLinkDirector
    • EventListener

      • EventListenerAcceleration
      • EventListenerController
      • EventListenerCustom
      • EventListenerFocus
      • EventListenerKeyboard
      • EventListenerMouse
      • EventListenerTouchOneByOne
    • Event
      • EventDispatcher
      • EventController
      • EventFocus
      • EventCustom
      • EventKeyboard
      • EventMouse
      • EventTouch
    • Scheduler
    • ScriptHandlerEntry
    • Touch
    • ActionManager
    • Action : Cloneable
      • FiniteTimeAction
        • ActionInstant
          • StopGrid
        • ActionInterval
          • GridAction
            • Grid3DAction
            • TiledGrid3DAction
            • AccelDeccelAmplitude
          • AccelAmplitude
          • DeccelAmplitude
          • ActionEase
            • EaseBackIn
            • EaseBackInOut
            • EaseBackOut
            • EaseBezierAction
            • EaseBounce
              • EaseBounceIn
              • EaseBounceInOut
            • EaseCircleActionIn
            • EaseCircleActionInOut
            • EaseCircleActionOut
            • EaseCubicActionIn
            • EaseCubicActionInOut
          • Animate
          • BezierBy
          • BezierTo
          • Blink
          • FadeTo
            • FadeIn
            • FadeOut
          • JumpBy
            • JumpTo
          • MoveBy
            • MoveTo
          • Repeat
          • RepeatForever
          • ReverseTime
          • RotateBy
          • RotateTo
          • ScaleTo
            • ScaleBy
          • TintBy
          • TintTo
          • TargetedAction
          • ProgressFromTo
          • ProgressTo
          • ActionTween
        • Sequence
        • SkewTo
          • SkewBy
        • Spawn
      • Follow
      • Speed
      • ActionCamera
        • OrbitCamera
    • PointArray
    • AnimationFrame : Cloneable
    • Animation : Cloneable
    • AnimationCache
    • Node
      • Camera
      • BaseLight
        • AmbientLight
        • DirectionLight
        • PointLight
        • SpotLight
      • AtlasNode
        • LabelAtlas : LabelProtocol
        • TileMapAtlas
      • ClippingNode
      • ClippingRectangleNode
      • DrawNode
      • TMXLayer
      • TMXTiledMap
      • LabelTTF : LabelProtocol , BlendProtocol
      • LabelBMFont : LabelProtocol , BlendProtocol
      • Layer
        • LayerMultiplex
        • LayerColor : BlendProtocol
          • LayerGradient
        • Menu
        • MenuItem
        • ImageView
          • MenuItemSprite
          • MenuItemToggle
          • MenuItemLabel
            • MenuItemAtlasFont
            • MenuItemFont
            • MenuItemFont
      • MotionStreak : TextureProtocol
      • NodeGrid
      • RenderTexture
      • ParallaxNode
      • ProtectedNode
        • Widget : LayoutParameterProtocol
          • Button
          • CheckBox
          • LoadingBar
          • Slider
          • Text
          • TextAtlas
          • TextBMFont
          • VideoPlayer
          • WebView
          • Layout : LayoutProtocol
            • RelativeBox
            • PageView
            • HBox
            • VBox
            • ScrollView
              • ListView
      • ParticleBatchNode : TextureProtocol
      • ParticleSystem : TextureProtocol
        • ParticleSystemQuad
          • ParticleExplosion
          • ParticleMeteor
          • ParticleFire
          • ParticleFireworks
          • ParticleFlower
          • ParticleGalaxy
          • ParticleRain
          • ParticleSmoke
          • ParticleSnow
          • ParticleSpiral
          • ParticleSun
      • Scene
        • TransitionScene
          • TransitionCrossFade
          • TransitionFade
          • TransitionJumpZoom
          • TransitionShrinkGrow: TransitionEaseScene
          • TransitionMoveInL: TransitionEaseScene
            • TransitionMoveInB
            • TransitionMoveInR
            • TransitionMoveInT
          • TransitionSceneOriented
            • TransitionFlipAngular
            • TransitionFlipX
            • TransitionFlipY
            • TransitionZoomFlipX
            • TransitionZoomFlipY
          • TransitionFadeTR : TransitionEaseScene
            • TransitionFadeDown
            • TransitionFadeUp
          • TransitionSlideInL: TransitionEaseScene
            • TransitionSlideInR
            • TransitionSlideInT
          • TransitionSplitCols: TransitionEaseScene
            • TransitionSplitRows
          • TransitionTurnOffTiles
          • TransitionPageTurn
          • TransitionProgress
      • Sprite : TextureProtocol
      • SpriteBatchNode : TextureProtocol
        • TMXLayer
      • TMXTiledMap
    • Component
      • FontCharMap
      • FontFNT
      • FontFreeType
      • LayoutComponent
    • FontAtlas
    • Grabber
    • GridBase
      • Grid3D
      • TiledGrid3D
    • SpriteFrame : Clonable
    • SpriteFrameCache
    • TMXObjectGroup
    • TMXLayerInfo
    • LayoutManager
    • RichElement
    • GLView
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值