'SDL_CreateTextureFromSurface: surface is invalid' + EXC_Bad_Access

Hello!

I’m making a game in SDL, I’m trying to load a font using SDL_ttf so I can render text. Upon running the code to render said text, I get hit with an error. In the IDE itself on a line where I defined the rect, I got an error “Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)”. This also had a red underline on the 3rd perimeter which is for the width of the rect. I’m assuming the same could go for the 4th perimeter for height.

In the terminal itself, I got an SDL_ttf error.

“‘SDL_CreateTextureFromSurface(): surface’ is invalid.”

This appears twice, as I tried loading text twice.

Codewise, this is a snippet of the class I had the code for loading an image and rendering text

class diaText {
    public:
        SDL_Surface* diaName1;
        SDL_Surface* mainDiaText1;
        
        SDL_Texture* diaNameTexture1;
        SDL_Texture* mainDiaTextTexture1;
        
        diaText() {
            diaName1 = TTF_RenderText_Solid(mainFont50, "Moses", cWHITE);
            mainDiaText1 = TTF_RenderText_Solid(mainFont50, "Testing Testing 1, 2 ,3", cWHITE);
            
            
            diaNameTexture1 = SDL_CreateTextureFromSurface(gRenderer, diaName1);
            if (!diaName1) {
                std::cout << "Error rendering text SDL_ttf:" << TTF_GetError() << std::endl;
            }
            
            mainDiaTextTexture1 = SDL_CreateTextureFromSurface(gRenderer, mainDiaText1);
            if (!mainDiaText1) {
                std::cout << "Error rendering text SDL_ttf" << TTF_GetError() << std::endl;
            }
        }
        
        void render() {
            SDL_Rect diaName1Rect = {100, 100, diaName1->w, diaName1->h}; // error on this line
            SDL_Rect mainDiaText1Rect = {200, 200, mainDiaText1->w, mainDiaText1->h};
            
            SDL_RenderCopy(gRenderer, diaNameTexture1, NULL, &diaName1Rect);
            SDL_RenderCopy(gRenderer, mainDiaTextTexture1, NULL, &mainDiaText1Rect);
        }
    };

In the main function, I call all the functions in the classes.

        dialouge dia;
        dialouge::diaText diaText;

(Insert code in between here)

        SDL_RenderClear(gRenderer);
        bg.drawBackground();
        moses.render();
        dia.render();
        diaText.~diaText();
        diaText.render();

        SDL_RenderPresent(gRenderer);

Yes, my renderer is working, it’s in a header file separate from this main cpp file.

For any more information, reply please!

Xcode 16.2
SDL2
SDL_ttf 2

(unrelated)
SDL_image 2

Thank you for reading!

Check for null and print the error messages right after calling TTF_RenderText_Solid

diaName1 = TTF_RenderText_Solid(mainFont50, "Moses", cWHITE);
if (!diaName1) {
    std::cout << "Error rendering text SDL_ttf:" << TTF_GetError() << std::endl;
}

mainDiaText1 = TTF_RenderText_Solid(mainFont50, "Testing Testing 1, 2 ,3", cWHITE);
if (!mainDiaText1) {
    std::cout << "Error rendering text SDL_ttf" << TTF_GetError() << std::endl;
}

… otherwise the TTF_GetError() message will be overwritten by later function calls.

Yep. Doing this gave me an error that a Null pointer was passed.

If the TTF_Font pointer is null then you should check the error message after TTF_OpenFont. Maybe it’s a problem with the path.


That you’re calling the destructor manually looks suspicious. I don’t know if it causes any problems here but it’s normally something that you should never have to do. The destructor will be called automatically when the variable goes out of scope (even if you called the destructor earlier). The destructor destroys the object so technically you’re not allowed to call its member functions or access its member variables unless you construct it again.