If you want to crash your emulator or device with an out of memory error, run the following code, clicking “Click me!” repeatedly until the app crashes.
This code below is loosely based on something I am attempting to do in a real app. I have a score counter inside of a snapshot specifically so that I can apply the bloom filter and get a nice glow effect. In my game, as the score goes up, so does the memory. In my game, I am actually using embossed text but I use standard text below to ensure that the embossed text is not related to the memory issue. To simulate the score doing up in the code below, I added a simple widget button that, when clicked, increments a counter, invalidates the snapshot, and re-applies the filter. (You must re-apply a filter to a snapshot after it’s invalidated or else it won’t take effect.)
Memory is not an issue if the standard blur or brightness filters are used instead. To verify this, change the code in the applyFilter function accordingly. You might also try other filters just to see what they do. I have only tried these four. (See code below.)
The simple red text I used below doesn’t really look special with the bloom filter, but keep in mind that I am just trying to reproduce the memory issue here. A well-used bloom filter looks really cool.
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local widget = require("widget") local function applyFilter(snapshot) --these filters leak memory! snapshot.fill.effect = "filter.bloom"; --snapshot.fill.effect = "filter.blurGaussian"; --these filters do NOT leak memory --snapshot.fill.effect = "filter.blur"; --snapshot.fill.effect = "filter.brightness"; end local counter = 0; local counterTxt = display.newText( '0', display.contentWidth / 2, 20, native.systemFont, 300 ) counterTxt.fill = {1,0,0} local snapshot = display.newSnapshot(display.contentWidth \* 2, 300) snapshot.y = 100 snapshot.group:insert(counterTxt); applyFilter(snapshot); local function handleButtonEvent( event ) if ( "ended" == event.phase ) then counter = counter + 1; counterTxt.text = counter; snapshot:invalidate( ) --must re-apply filter after invalidating snapshot applyFilter(snapshot); end end -- Create the widget local memoryKillerBtn = widget.newButton { id = "memoryKiller", label = "Click me!", onEvent = handleButtonEvent } memoryKillerBtn.x = display.contentWidth / 2; memoryKillerBtn.y = 280;