Skip to content

Conversation

cyn0x8
Copy link
Contributor

@cyn0x8 cyn0x8 commented Aug 6, 2025

currently the game rapidly leaks memory if 5 conditions are met:

  • using renderTile
  • some camera bgColor alpha is 0
  • no sprites or anything being rendered to this camera (nothing in draw stack)
  • at least one BitmapFilter applied to this camera
  • no flash fx or color fade fx active on this camera

unsure of exactly what the lower-level issue is causing this bug, but my guess is that after the canvas.graphics is cleared in CameraFrontEnd.lock, due to the above conditions its never drawn to again before the next frame, and so the filters are put in a weird state and somehow memory is leaked

this pr fixes the issue by adding a force option to FlxCamera.fill to bypass the alpha early-return for renderTile, and forcing a bgColor fill for it if any filters are currently active on the camera regardless of the alpha value


to reproduce the issue:

package;

import openfl.filters.BitmapFilter;

import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;

class TestState extends FlxState {
	override function create():Void {
		super.create();
		
		add(new FlxSprite().makeGraphic(1, 1, 0x0));
		// ^ remove this line and the game will leak memory like crazy
		
		var cam:FlxCamera = new FlxCamera();
		cam.bgColor = 0x0;
		FlxG.cameras.add(cam, true);
		cam.filters = [new BitmapFilter()];
	}
}

before and after fix:

Screenshot 2025-08-06 153823 image

@cyn0x8 cyn0x8 changed the title fix bitmapfilter memory leak fix camera filter memory leak Aug 7, 2025
@cyn0x8
Copy link
Contributor Author

cyn0x8 commented Aug 7, 2025

extra arg is causing issues... ill figure out a different way soon
trying to avoid just putting the filter check at the alpha early-return because fill isnt only used for bgColor; idk if the check there would be acceptable or not
edit: couldnt figure out another way, other than for bgColor fill only seems to be used internally for flash/color fade fx, and these dont call fill when the alpha is 0 anyways

@cyn0x8 cyn0x8 marked this pull request as draft August 7, 2025 00:46
@cyn0x8 cyn0x8 marked this pull request as ready for review August 7, 2025 01:50
@Geokureli
Copy link
Member

Geokureli commented Aug 14, 2025

Tried this change on html5 hl and mac native and saw no difference. I see no memory leak on hl or native (mac) with or without this change, regardless of whether there is a sprite drawn. I do see memory climbing in html5 to a GC call but I also see that in a blank project (I should make an issue for that). For what targets were you seeing a memory leak?

also, what purpose does the FxAlpha == 0 check serve? can that be taken out entirely?

@cyn0x8
Copy link
Contributor Author

cyn0x8 commented Aug 14, 2025

i know for sure the leak happens on cpp windows; i didnt see it happen on hl windows or html5

and as for the FxAlpha == 0 i believe its intended to be an optimization? an early return skipping fully transparent cam fills
i didnt remove it in case it had to be kept, but if its acceptable i can remove the check entirely

@Geokureli
Copy link
Member

i know for sure the leak happens on cpp windows; i didnt see it happen on hl windows or html5

I'll try on windows

and as for the FxAlpha == 0 i believe its intended to be an optimization? an early return skipping fully transparent cam fills i didnt remove it in case it had to be kept, but if its acceptable i can remove the check entirely

I'm leaning towards removing it entirely, we don't really need to optimize, when nothing is being drawn

LeonGamerPS1 added a commit to Graveyard-Forks/snowgraved-flixel that referenced this pull request Sep 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants