Away3D 的实体收集器Bug
最近在改Away3D源碼的時(shí)候遇到個(gè)很郁悶的問(wèn)題,發(fā)現(xiàn)創(chuàng)建的Mesh 釋放不掉。
分析源碼發(fā)現(xiàn) EntityListItemPool 類中邏輯Bug在getItem()函數(shù)中發(fā)現(xiàn)_poolSize 對(duì)象池大小如果夠用的情況下 它采用的方式是復(fù)用EntityListItem
那么假設(shè)我刪除了場(chǎng)景上有10個(gè)對(duì)象我全部刪除了然后我再創(chuàng)建9個(gè) 這時(shí)候總有1個(gè)對(duì)象是被緩存著的。一直要等到我創(chuàng)建第10個(gè)對(duì)象他才會(huì)被釋放掉。
沒(méi)轍了跑到 看看對(duì)象銷毀流程吧。
對(duì)象被銷毀時(shí)會(huì)調(diào)用 Scene3D 的 unregisterEntity函數(shù),這個(gè)函數(shù)只是刪除了Scene3D 和 顯示對(duì)象的引用。但是EntityListItemPool中還是存在實(shí)例引用
?看了看收集器 每次都要經(jīng)過(guò) Scene3D 的 traversePartitions函數(shù)。
那我先在unregisterEntity函數(shù)調(diào)用的時(shí)候做一次記錄把要?jiǎng)h除的顯示對(duì)象添加到一個(gè)列表中。
private var _unregisterEntityList:Vector.<Entity> = new Vector.<Entity>;
??/**
?? * When an entity is removed from the scene, or from one of its children, remove it from its former partition tree.
?? * @private
?? */
??arcane function unregisterEntity(entity : Entity) : void
??{
???_unregisterEntityList.push(entity);
???entity.implicitPartition.removeEntity(entity);???
??}
這樣在下一幀執(zhí)行渲染的時(shí)候我就知道要釋放掉哪些對(duì)象了。然后修改traversePartitions函數(shù).
public function traversePartitions(traverser : PartitionTraverser) : void
??{
???var i : uint;
???var len : uint = _partitions.length;
???if(traverser is EntityCollector)
???{
????while(_unregisterEntityList.length)
????{
?????var _entity:Entity = _unregisterEntityList.shift();
?????(traverser as EntityCollector).entityListItemPool.unmap(_entity);
?????(traverser as EntityCollector).renderableListItemPool.unmap(_entity);
????}
???}
???traverser.scene = this;
???while (i < len)
????_partitions[i++].traverse(traverser);
??}
每次在新的一輪收集前把上一幀要清楚掉的對(duì)象全部干掉。
然后跑到entityListItemPool 類里添加一段代碼:
public function unmap(mesh:Entity) : void
??{
???var _mesh:Mesh = mesh as Mesh;
???for(var j:int =0;j<_mesh.numSubMesh;j++)
???{
????for(var i:int = 0; i < _pool.length; i++)
????{
?????if((_pool[i].renderable is SubMesh) && (_pool[i].renderable as SubMesh).parentMesh && (_pool[i].renderable as SubMesh).parentMesh == _mesh)
?????{
??????(_pool[i].renderable as SubMesh).parentMesh = null;
??????_pool.splice(i,1);
??????_poolSize --;
??????continue;
?????}
????}
???}
??}
?
這樣保證了對(duì)象的釋放。OK 這下好了
測(cè)試下了一下沒(méi)有問(wèn)題全部乖乖的垃圾回收了。
轉(zhuǎn)載于:https://www.cnblogs.com/ch06src/p/3874428.html
總結(jié)
以上是生活随笔為你收集整理的Away3D 的实体收集器Bug的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php fpm安装curl后,nginx
- 下一篇: myeclipse tomcat内存溢出