🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

SDL Functions failing and causing segfaults, and I don't know why

Started by
1 comment, last by Aldacron 6 years, 11 months ago

I am attempting to make a Pong game using SDL. I'm trying to render text on the screen, but segfaults prevent the code from running. I ran it in gdb, and found that the segfaults occur because SDL_QueryTexture doesn't give the ints passed into it any values. I put a statement to print SDL_GetError() after SDL_QueryTexture, and it says: "Failed loading SHCORE.DLL: The specified module could not be found." This wasn't happening after I made some adjustments to my code, so I believe the code is causing this problem. The relevant code is below:

 

In case the rest of the code has a part in this, the entire .cpp file is attached.


	void loadText(string s) {
            SDL_Color color = {255,255,255};
            SDL_Surface* textsurface = TTF_RenderText_Solid(font, s.c_str(), color);
            if(textsurface == NULL) {
                cout << "Could not rendertext onto surface" << endl;
            }
            texture = SDL_CreateTextureFromSurface(r,textsurface);
            if(texture == NULL) {
                cout << "Could not make texture" << SDL_GetError() << endl;
            }
            SDL_QueryTexture(texture, NULL, NULL, w, h);
            cout << SDL_GetError() << endl;
            SDL_FreeSurface(textsurface);
            textsurface = NULL;
            
                //cout << "Texture query error" << SDL_GetError() << endl;
            
        }
	void textRender(int x, int y) {
            
            destinationrect = {x,y,*w,*h};
            if (SDL_RenderCopy(r,texture,NULL,&destinationrect) < 0) {
                cout << "Rendercopy error" << SDL_GetError() << endl;
            }
            
            
        }
	 
	

test.cpp

OpenSans-Regular.ttf

Advertisement

I doubt the error message is coming from SDL_QueryTexture (it shouldn't be attempting to load any DLLs). I see two problems with your code that you need to correct before reaching that conclusion.

First, you aren't checking the return value of the function call. The function is documented to return zero on success and a negative error code on failure.

Second, based on your usage of w and h in textRender, it seems you've declared both as pointers. This is likely the source of your segfault. SDL_QueryTexture does not store pointers to ints in the width and height parameters. It stores the actual values of the width and height. That means you have to pass it the addresses of actual int variables, not uninitialized int pointers. In other words:


// This is certain to cause a segault
int *w, *h;
SDL_QueryTexture(tex, NULL, NULL, w, h);

The correct way to do this is like so:


int w = 0, h = 0;
if(SDL_QueryTexture(tex, NULL, NULL, &w, &h) < 0)  {
  fprintf(stderr, "Error on texture query: %s", SDL_GetError());
}

 

This topic is closed to new replies.

Advertisement