views:

570

answers:

2

The manual just instructed you to write:

glDeleteTextures(1, &GLtexture);

and claims that the texture will be deleted. iPhone has scarce memory and I want to ensure that these textures are truly released.

The Leaks instrument cannot detect this, and frankly, I am a bit worried. I do really want to make sure that the textures are really gone.

Thank you.

A: 

Wrap the calls to glDeleteTextures(int, int*) in your own function that maintains an allocation table.

I've done all those in object-oriented way already, but my point is: is it possible to check manually for the textures?
unknownthreat
Then it ain't purely Open GL-related anylonger, is it? If you have your textures referenced as texture objects, why don't create a global counter that you increase for each instantiated texture object, and decrease for each deleted texture. Then it remains no harder than to instrument a single variable...
Well I have managed these glGenTextures and glDeleteTextures on my own, but the thing is this: Because there's nothing returned from the function like whether deleting texture was a success or not, I feel quite anxious. 24 MB is very tiny especially when you have to expand all those compressed images into the RAM. I have to be anxious and I want to know whether the program does really successfully deallocate the textures. I do not want to find out later that my textures never get deallocated. You may think I worry too much, but memory is a very serious issue.
unknownthreat
+2  A: 

Textures aren't handled by the Obj-c runtime, so leaks doesn't know anything about them. You have to use another tool.

In Xcode start a new project using the OpenGL template. Search the method that updates/draws the scene and add this code to its end:

static int tick = -1;
static GLuint tex[5];

if (tick++ < 0)
    for (int f = 0; f < 5; f++)
        tex[f] = 0;

tick = tick % 5;
if (tex[tick]) {
    glDeleteTextures(1, &tex[tick]);
    tex[tick] = 0;
} else {
    glGenTextures(1, &tex[tick]);
    glBindTexture(GL_TEXTURE_2D, tex[tick]);
    char *mem = malloc(1024 * 1024 * 4);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
        1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, mem);
    free(mem);
}

The code generates and destroys five textures using OpenGL Commands. Build it and run it once on your device to make sure it is installed. Don't worry about the chugging.

Now open instruments and start with the blank template for the iPhone. Open the library and drag the Memory Monitor to the window. Click on the information disclose button and uncheck everything but "Physical Memory Free". Now select to launch the binary on your iphone to start recording. You should see staircase pattern going up/down depending on when Instruments tries to sample the application. While the program is running you can see all the active processes with the column "Real Memory", showing actual memory usage.

In my tests, this example consumes between 25MB to 3MB depending on the moment of the memory sampling. This is with a second generation iPhone and SDK 3.1. If you have a 2.x SDK you have to search for the GART Resident Object Size in the normal OpenGL monitor. See http://blog.zincroe.com/2009/04/how-to-check-iphone-texture-memory-usage-with-instruments/ for further reference.

In any case, the memory jumping up and down proves that glDeleteTexture() does its work as advertised.

Grzegorz Adam Hankiewicz