Gamua / Starling-Framework

The Cross Platform Game Engine
http://www.starling-framework.org
Other
2.82k stars 821 forks source link

DisplayObject dispose() method doesn't automatically remove the object from its parent #1085

Closed Adolio closed 3 years ago

Adolio commented 3 years ago

Hi Daniel,

Maybe that's a misunderstanding from my side, I just discovered that the DisplayObject::dispose() method is not (always?) removing the object from its parent.

I noticed that when disposing a ParticleSystem with a Filter applied on it. Calling myParticleSystemWithAFilter.dispose() is causing the following error at next render pass:

TypeError: Error #2007: Parameter indexBuffer must be non-null.
    at flash.display3D::Context3D/drawTriangles()
    at starling.rendering::Effect/render()[/Users/redge/Development/starling/starling/src/starling/rendering/Effect.as:236]
    at starling.extensions::ParticleSystem/render()[c:\Users\dacampo\Development\AIR\Starling-Extension-Particle-System\src\starling\extensions\ParticleSystem.as:323]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.filters::FragmentFilter/renderPasses()[/Users/redge/Development/starling/starling/src/starling/filters/FragmentFilter.as:254]
    at starling.filters::FragmentFilter/render()[/Users/redge/Development/starling/starling/src/starling/filters/FragmentFilter.as:161]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:395]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.core::Starling/render()[/Users/redge/Development/starling/starling/src/starling/core/Starling.as:460]
    at starling.core::Starling/nextFrame()[/Users/redge/Development/starling/starling/src/starling/core/Starling.as:407]
    at starling.core::Starling/onEnterFrame()[/Users/redge/Development/starling/starling/src/starling/core/Starling.as:635]

👉 However, when doing myParticleSystemWithAFilter.removeFromParent(true) instead, everything is fine 👍


Is there any particular reason to keep a disposed DisplayObject attached to its parent (and therefore potentially in the render list)?

Perhaps that's only an issue in the Starling-Extension-Particle-System extension?

Thanks in advance for your help 🙂

PrimaryFeather commented 3 years ago

Hi Aurélien!

That is a misunderstanding, yes — disposing a display object won't automatically remove it from its parent. Doing removal and disposal at the same time, that's what removeFromParent(true) is for — just like you're calling it.

It's not far-fetched to think that it would remove it, though — it could have been implemented that way. I think the reason I didn't was just that I like to keep responsibilities separated – a method shouldn't do unexpected things, and the name dispose doesn't say anything about removing an object from the display list. Doing it the other way around (removeFromParent(true)) just seems more explicit to me.

Adolio commented 3 years ago

Thanks Daniel for the fast & clear explanations!

That's clearer for me now 🙂

PrimaryFeather commented 3 years ago

You're welcome, Aurélien! I'm glad I could help! 😄