c – SDL2 texture is sometimes empty after loading multiple 8-bit surfaces

I will try to make the question as concise as possible, but feel free to ask for clarification.

I process legacy code and try to load thousands of 8 bit images from disk to create a texture for each.

I've tried several things and I'm about to try loading my 8-bit images on a 32-bit surface and then creating a texture from that surface.

The problem: when loading and 8-bit image on a 32-bit surface, when I try to SDL_CreateTextureFromSurface, I end up with a lot of completely empty textures (full of transparent pixels, 0x00000000).

All the textures are not wrong, thought. Every time I run the program, I get different "bad" textures. Sometimes there is more, sometimes there is less. And when I draw the program, I always finish with a correct texture (is it a sync problem?)

I know that the loading at the SDL_Surface works, because I save all the surfaces on the disk and they are all correct. But I inspected the textures using NVidia NSight Graphicsand more than half are virgins.

Here is the offending code:

int __cdecl IMG_SavePNG (SDL_Surface *, const char *);

SDL_Texture * Resource8bitToTexture32 (rendering SDL_Renderer *, palette SDL_Color *, int paletteSize, void * dataAddress, int Width, int Height)
u32 uiCurrentOffset;
u32 uiSourceLinearSize = (Width * Height);
SDL_Color * currentColor;
strSurfacePath char[500];

// The texture we create
SDL_Texture * newTexture = NULL;

// Load the image at the specified address
SDL_Surface * tempSurface = SDL_CreateRGBSurface (0x00, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);

SDL_SetSurfaceBlendMode (tempSurface, SDL_BLENDMODE_NONE);

SDL_LockSurface (tempSurface);

// Set the surface in transparent pixels
SDL_memset (tempSurface-> pixels, 0, Height * tempSurface-> pitch);

for (uiCurrentOffset = 0; uiCurrentOffset < uiSourceLinearSize; uiCurrentOffset++)
    currentColor = &palette[pSourceData[uiCurrentOffset]];
    if(pSourceData[uiCurrentOffset] != PC_COLOR_TRANSPARENT)
      ((u32*)tempSurface->pixels)[uiCurrentOffset] = (u32) ((currentColor-> a << 24) + (currentColor->r< 16) + (currentColor->g << 8) + (currentColor->b << 0));
SDL_UnlockSurface (tempSurface);

// Create a texture from surface pixels
newTexture = SDL_CreateTextureFromSurface (rendering, tempSurface);

// save the surface to disk for verification only
sprintf (strSurfacePath, "c: \ tmp \ surfaces \% s.png", GenerateUniqueName ());
IMG_SavePNG (tempSurface, strSurfacePath);

// get rid of the old loaded surface
SDL_FreeSurface (tempSurface);

return newTexture;

Note that in the original code, I check the limits, and for NULL after the SDL_Create *. I am also aware that it would be better to have a texture map sheet than to load each texture individually.