I'm writing a pure C game engine. I assert every pointer returned by malloc() in my code. Now I'm writing a function that will "slice" a loaded texture into new textures (parts of the parent texture). The parent texture is loaded correctly (other textures loaded by the same loader were fine, all of them are TGA images).
I'm just going to paste the bit giving me trouble, the engine is too big.
// ...
typedef struct
{
float W; // Texture width
float H; // Texture height
unsigned char BytesPerPixel; // Bytes per pixel
int Texels; // Amount of texels
unsigned char* TexelsPtr; // Pointer to texels array
// ...
} CTexture;
// ...
// This is the function argument, allocated outside of the function (with an assert).
CTexture* OutputPtr;
// Other arguments and function body starts.
// Things like checking if the parent texture is big enough, etc.
// A loop body begins here
// .W, .H and .BytesPerPixel have valid values, which are given here.
OutputPtr[i].Texels = OutputPtr[i].W * OutputPtr[i].H * OutputPtr[i].BytesPerPixel;
OutputPtr[i].TexelsPtr = NULL;
OutputPtr[i].TexelsPtr = malloc(sizeof(unsigned char) * OutputPtr[i].Texels);
assert(OutputPtr[i].TexelsPtr);
// I copy the texels from parent texture here
// ... Function body ends
I get an assertion error when i == 20 (i < 96).
I checked for memory leaks with UMDH (a Microsoft tool; comes with debugging tools) and found none.
Any ideas?
Thanks
Marek