Gamua / Starling-Framework

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

QuadBatch null pointer exception #937

Closed greymag closed 7 years ago

greymag commented 7 years ago

There is a problem In branch origin/v1.8.x after replace Starling event with conventional in QuadBatch. Now, if someone dispose QuadBatch in it's own context created listener, then QuadBatch will thrown 1009 error in createBuffers() (called from onContextCreated). Thats because removeEventListener() are not remove event listener while handle this event

PrimaryFeather commented 7 years ago

Thanks for the report! Could you maybe create a sample that I can use to reproduce the problem? I'm not sure I understand it 100% by the description alone.

greymag commented 7 years ago

Yes, of course. The code below will throw null pointer exception after 500 ms delay

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.utils.setTimeout;
    import starling.core.Starling;
    import starling.display.QuadBatch;
    import starling.events.Event;

    public class TestStarling extends flash.display.Sprite
    {
        private var _starling:Starling;
        private var _qb:QuadBatch;

        public function TestStarling()
        {
            Starling.handleLostContext = true;

            _starling = new Starling(starling.display.Sprite, this.stage);

            _starling.addEventListener(starling.events.Event.ROOT_CREATED, this.rootCreated);
            _starling.start();
        }

        private function rootCreated(e:*):void
        {
            _starling.stage3D.addEventListener(flash.events.Event.CONTEXT3D_CREATE, this.onContextCreated);
            _qb = new QuadBatch();

            setTimeout(function ():void {
                // initiate context lost
                _starling.context.dispose();
            }, 500)
        }

        private function onContextCreated(e:*):void
        {
            // dispose QuadBatch on context lost
            _qb.dispose();
        }
    }
}
PrimaryFeather commented 7 years ago

I just added a check that should avoid these problems. Sorry for the delay!