Invalid Native Object

Invalid Native Objectで怒られる

既に既出だと思いますが、
Cocos2d-JS(html5)をiOSコンパイルしようとした場合のメモリの問題。
http://www.cocos2d-x.org/wiki/Memory_Management_of_JSB

Cocos2d-x (C++) のメモリ管理はretainカウント方式なので生じるメモリ管理の問題。
メモリ確保したときに、非表示にして、後で使いたいときにaddChildしようとしてもthisで呼べず「Invalid Native Object」と怒られます。

メニュー画面など隠しておく必要がある場合はもちろん、
特にこの問題は下記のようなアニメーションを制御しようとしたときに、顕著に起こります。

今回起きた例

<やろうとしたこと>
リピートがisDoneしたことを確認後、スプライトを削除したい。

...ところが、
いざ使おうとすると、

1.リピート終了を検知(isDone)する時点でthis.repがない..
2.this.spもない..

ということで、
1.cc.Repeat.createした rep を isRetain()しておく
2.removeするためにsp自身もretainしておく必要がある
3.最後にreleaseする

//sprite sheetを分割
var frameSeq = [];
for (var i = 0; i <= 4; i++) {
    var frame = cc.SpriteFrame.create(s_break,cc.rect(48*i,0,48,96));
    frameSeq.push(frame);
}
//sprite sheet -> animation
this.wa = cc.Animation.create(frameSeq,0.1);
//リピート×1
this.rep = cc.Repeat.create(cc.Animate.create(this.wa),1);
//★1 this.rep.retain();
//spriteを作成
this.sp = cc.Sprite.create(s_coin,cc.rect(0,0,48,96));
//★2 this.sp.retain();
this.sp.runAction(this.rep);
this.addChild(this.sp);

//updateの中で..
if(this.rep.isDone() == true){
	//★3 this.rep.release(); this.sp.release();
    this.removeChild(this.sp);
}