PDA

View Full Version : Freeing loaded texture



pion
12-23-2010, 04:33 PM
I am looking at the Textured Box demo on http://www.khronos.org/webgl/wiki/Demo_Repository. It has the following code snippets:


function loadImageTexture(ctx, url)
{
var texture = ctx.createTexture(); // allocate texture
texture.image = new Image();
texture.image.onload = function() { doLoadImageTexture(ctx, texture.image, texture) }
texture.image.src = url;
return texture;
}

function doLoadImageTexture(ctx, image, texture)
{
ctx.bindTexture(ctx.TEXTURE_2D, texture);
ctx.texImage2D(ctx.TEXTURE_2D, 0, ctx.RGBA, ctx.RGBA, ctx.UNSIGNED_BYTE, image); // loaded the image
...
}
...

var spiritTexture = loadImageTexture(gl, "resources/spirit.jpg");

How do you free allocated/loaded texture in order to avoid memory leak?

Will the following code free both the loaded/allocated texture and image?

spiritTexture = null;
Thanks in advance for your help.

pion
01-23-2011, 07:57 PM
http://stackoverflow.com/questions/4776 ... e-on-webgl (http://stackoverflow.com/questions/4776913/freeing-loaded-texture-on-webgl) answers my question.

ctx.deleteTexture(spiritTexture);