[mono-android] Deleting textures to free up resources

Kestutis Tauckela kestutis.tauckela at gmail.com
Thu Mar 10 15:53:10 EST 2011


Hey again,

It looks like you should call bitmap.Dispose() instead of bitmap.Recycle()
for clean release of the resources.

Many thanks to ankit9 and jonp from MonoDroid chat for helping me to solve
this!

Cheers!
Kestutis Tauckela.
http://www.outlanderstudios.com/

On Thu, Mar 10, 2011 at 12:03 AM, Kestutis Tauckela <
kestutis.tauckela at gmail.com> wrote:

> Hi,
>
> I'm having problems deleting old textures via OpenGL ES. To test that, I
> was using modified TexturedCube example. You can find original example here:
> https://github.com/mono/monodroid-samples/tree/master/TexturedCube.
> The only difference I made was loading (creating) textured and destroying
> them each time the cube was rendered. It was working slower (as expected),
> but if the texture would be destroyed properly - the exmaple would still run
> indefinitelly. Currently while using 256x256 texture it crashes after
> exactly 130 successful creations and destructions of said textures (on
> GalaxyS phone). Further tests shown that texture destruction code can be
> skipped entirely and still produce same results. Which makes me think that
> there might be some issues in destruction of the textures.
>
> Am I missing some destruction call to clean-up properly? Or is it a bug?
>
> BTW, the crash happens in the "bitmap.Recycle();" call. It looks like
> bitmap object is not null, but not valid either. Is it another bug in
> BitmapFactory.DecodeResource?
>
> Thank you,
> Kestutis Tauckela.
> http://www.outlanderstudios.com/
>
> Here are main functions from the code I was using:
>         protected int LoadTexture (Context context, int resourceId)
>         {
>             int nTextureId = 0;
>
>             // create texture
>             GL.Enable(All.Texture2D);
>             GL.GenTextures(1, ref nTextureId);
>             GL.BindTexture(All.Texture2D, nTextureId);
>
>             // setup texture parameters
>             GL.TexParameterx(All.Texture2D, All.TextureMagFilter,
> (int)All.Linear);
>             GL.TexParameterx(All.Texture2D, All.TextureMinFilter,
> (int)All.Linear);
>             GL.TexParameterx(All.Texture2D, All.TextureWrapS,
> (int)All.ClampToEdge);
>             GL.TexParameterx(All.Texture2D, All.TextureWrapT,
> (int)All.ClampToEdge);
>
>             int ww, hh;
>             int [] pixels = GetTextureFromBitmapResource (context,
> resourceId, out ww, out hh);
>
>             GL.TexImage2D (All.Texture2D, 0, (int)All.Rgba, ww, hh, 0,
> All.Rgba, All.UnsignedByte, pixels);
>
>             return nTextureId;
>         }
>
>         protected void DestroyTexture(ref int nTextureID)
>         {
>             GL.DeleteTextures(1, ref nTextureID);
>         }
>
>         static int[] GetTextureFromBitmapResource(Context context, int
> resourceId, out int width, out int height)
>         {
>             Bitmap bitmap = null;
>             try {
>                 bitmap = BitmapFactory.DecodeResource(context.Resources,
> resourceId);
>
>                 width = bitmap.Width;
>                 height = bitmap.Height;
>
>                 int [] pixels = new int [width * height];
>
>                 // Start writing from bottom row, to effectively flip it in
> Y-axis
>                 bitmap.GetPixels  (pixels, pixels.Length - width, -width,
> 0, 0, width, height);
>                 return pixels;
>             }
>             finally    {
>                 if (bitmap != null) {
>                     bitmap.Recycle();
>                 }
>             }
>         }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110310/df19ef06/attachment.html 


More information about the Monodroid mailing list