Help: ReadPixels() Out of Memory?

Monkey Forums/Monkey Programming/Help: ReadPixels() Out of Memory?

Midimaster(Posted 2013) [#1]
I got a report in GooglePla that my app crashed during ReadPixels():

java.lang.OutOfMemoryError
at com.midimaster.ballerburgfull.gxtkGraphics.ReadPixels(MonkeyGame.java:1991)
at com.midimaster.ballerburgfull.bb_graphics.g_ReadPixels(MonkeyGame.java:12649)
at com.midimaster.ballerburgfull.bb_.g_ScreenGrab(MonkeyGame.java:12164)
at com.midimaster.ballerburgfull.c_Game.p_BergSkalieren(MonkeyGame.java:5567)
at com.midimaster.ballerburgfull.c_Game.p_OnRender(MonkeyGame.java:6510)
at com.midimaster.ballerburgfull.c_GameDelegate.RenderGame(MonkeyGame.java:7093)
at com.midimaster.ballerburgfull.BBGame.RenderGame(MonkeyGame.java:570)
at com.midimaster.ballerburgfull.BBAndroidGame.onDrawFrame(MonkeyGame.java:1265)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1244)


But I cannot see a problem in the code:

Method BergSkalieren:Void()
	PushMatrix
		Scale ScaleX,ScaleY
		SetColor 255,255,255
		Scale 2,2
		DrawImage OriginalBerg,0,0
		Berg=ScreenGrab(0,0,DeviceWidth(),DeviceHeight())
	PopMatrix
End


Function ScreenGrab:Image(X%,Y%,Width%,Height%)
		Local ScreenShot:Image
		ScreenShot = CreateImage(Width,Height)
		Local pixels:Int[] =New Int[Width*Height]
		ReadPixels(pixels, X, Y, Width,Height)
		ScreenShot.WritePixels(pixels, 0, 0, Width,Height)
		Return ScreenShot
End


What could be wrong? I grab the whole device screen. It only happened on one device of a user. I don't know the device.

In all our tests it works perfect. Is it necessary to dimension the array a little bit bigger then the image size?
Local pixels:Int[] =New Int[Width*Height+1]



Shinkiro1(Posted 2013) [#2]
How many time are you calling BergSkalieren()?
If you call it every frame that means it will create an image every frame and without using Discard() that might add up quickly.

Also you could just use the Scale command to scale the mountain. As long as you are not doing per pixel manipulation ReadPixels() will be a waste of performance.


Midimaster(Posted 2013) [#3]
I call it once at the beginning of the level.

Of course I composed the background with a lot of elements before grabbing it. "DrawImage OriginalBerg,0,0" is only a place holder here in this post.


Gerry Quinn(Posted 2013) [#4]
If it happened on one user's device, I think you should think about it, but not panic until it happens on more users' devices.

That doesn't mean it's not a real bug. Users often don't report bugs, so often when you hear of one, many people have already suffered it in silence. At the same time, even when it's a real bug, many users are probably not hitting it.

In a case like this, I would be inclined to wait for more reports. But if you can contact the user in question to find if it is persistent, you should do so.