Skip to content

Null Object Reference when destroying something with an FlxAnimation on finishCallback #2782

@Starveldt

Description

@Starveldt
  • Haxe version: 4.3.1
  • Flixel version: 5.3.0
  • OpenFL version: 9.2.1
  • Lime version: 8.0.1
  • Affected targets: Windows

I noticed this after upgrading every library & Haxe. Whenever an FlxSprite, or something with an FlxAnimationController gets destroyed (and even the FlxAnimationController itself getting destroyed) in a finishCallback function, it seemingly still updates after getting destroyed, and so throws a Null Object Reference error when trying to reference parent (now null). What confused me is how this used to work, but now it seemingly doesn't. I've got no further clue as to what could be causing the issue, other than just adding a simple null check to FlxAnimation's getCurrentFrameDuration() function. i.e:

function getCurrentFrameDuration()
{
	// The only change is "parent != null"
	final curframeDuration:Float = parent != null ? parent.getFrameDuration(frames[curFrame]) : 0;
	return curframeDuration > 0 ? curframeDuration : frameDuration;
}

Code snippet reproducing the issue:

package;

import flixel.FlxSprite;
import flixel.FlxState;

class PlayState extends FlxState
{
	override public function create():Void
	{
		var someSprite:FlxSprite = new FlxSprite();
		// insert animation code here
		someSprite.animation.finishCallback = function(anim:String) {
			trace('Finished animation');

			someSprite.kill();
			remove(someSprite, true);

			someSprite.destroy();
			someSprite = null;
		}
		
		add(someSprite);
		someSprite.animation.play('someAnimation', true);
	}
}

Error log:

Called from hxcpp::__hxcpp_main
Called from ApplicationMain::main ApplicationMain.hx line 26
Called from ApplicationMain::create ApplicationMain.hx line 135
Called from lime.app.Application::exec lime/app/Application.hx line 150
Called from lime._internal.backend.native.NativeApplication::exec lime/_internal/backend/native/NativeApplication.hx line 155
Called from lime._internal.backend.native.NativeApplication::handleRenderEvent lime/_internal/backend/native/NativeApplication.hx line 388
Called from lime.app._Event_lime_graphics_RenderContext_Void::dispatch lime/_internal/macros/EventMacro.hx line 91
Called from openfl.display.Stage::__onLimeRender openfl/display/Stage.hx line 2046
Called from openfl.display.Stage::__broadcastEvent openfl/display/Stage.hx line 1181
Called from openfl.display.DisplayObject::__dispatch openfl/display/DisplayObject.hx line 1399
Called from openfl.events.EventDispatcher::__dispatchEvent openfl/events/EventDispatcher.hx line 402
Called from flixel.FlxGame::onEnterFrame flixel/FlxGame.hx line 556
Called from flixel.FlxGame::step flixel/FlxGame.hx line 682
Called from flixel.FlxGame::update flixel/FlxGame.hx line 752
Called from flixel.FlxState::tryUpdate flixel/FlxState.hx line 220
Called from states.PlayState::update states/PlayState.hx line 2143
Called from states.MusicBeatState::update states/MusicBeatState.hx line 72
Called from flixel.addons.ui.FlxUIState::update flixel/addons/ui/FlxUIState.hx line 212
Called from flixel.group.FlxTypedGroup::update flixel/group/FlxGroup.hx line 170
Called from flixel.group.FlxTypedSpriteGroup::update flixel/group/FlxSpriteGroup.hx line 205
Called from flixel.group.FlxTypedGroup::update flixel/group/FlxGroup.hx line 170
Called from flixel.FlxSprite::update flixel/FlxSprite.hx line 758
Called from flixel.FlxSprite::updateAnimation flixel/FlxSprite.hx line 766
Called from flixel.animation.FlxAnimationController::update flixel/animation/FlxAnimationController.hx line 93
Called from flixel.animation.FlxAnimation::update flixel/animation/FlxAnimation.hx line 220
Called from flixel.animation.FlxAnimation::getCurrentFrameDuration flixel/animation/FlxAnimation.hx line 227
Critical Error: Uncatchable Throw: Null Object Reference

(small note: NativeApplication.hx line 388 is actually window.onRender.dispatch(window.context); - i modified NativeApplication to have better fullscreening but that's it)

Unless this was intentional, I've got no idea how to fix the issue beyond adding a null check to getCurrentFrameDuration() (and most likely other functions...)

Any help is appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions