Memory leak using glTexSubImage2D

In the course of my application I create a 2048x2048 texture which then gets updated periodically. Every few times this texture gets updated via a glTexSubImage2D call there is a significant hit on the available memory of the system which never appears to be released.

I've attached a full application that demonstrates how I'm using this texture in my real application and having tested it find that this has the same memory issue as my real application. Can anyone see something I'm not doing properly that might account for this memory growth?

It's ranging between 0 and 16,772 bytes, so it doesn't appear to be correlated to the size of the texture. <edit, I just saw it jump up a little over 1MB on a 'n' press, so I really don't know where the size of the mem leak is coming from>

It's looking like the memory leak is stemming from the ati drivers the device my app is running on is using. I'm examining some valgrind results at the moment to figure out more about it, while working on a work around. So far, deleting the texture then recreating it from scratch doesn't clear up the problem.

Using GL_RGB for a TexSubImage call, your driver must allocate a new buffer, expand (and possibly reswizzle) your data to 32-bit into that new buffer, then transfer that new buffer to your GPU. Making a wild guess, your driver is failing to properly free the new buffer when done; it may not be even leaking - it may just be keeping that buffer around in case it needs to reuse it later in order to save the cost of a new memory allocation sometime in the future.

In general, you should use GL_BGRA for the format param of TexSubImage calls; this is more likely to match what the driver and hardware are actually using internally, and get you a direct transfer without the intermediate software step. You may or may not also need to use GL_UNSIGNED_INT_8_8_8_8_REV for the type param. It's worth making this switch to see if your observed leak goes away, and as a bonus it will get you higher performance for the data transfer itself.