Jump to content
  • Advertisement
CodeBro1

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

This topic is 516 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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());
}

 

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!