[SDL] Access Violation error - SDL_ttf

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

Recommended Posts

So, I'm busy making a (small) game for school.
And now I'm trying to display text, which should be easy.. This is my code:
 if(TTF_Init() == -1) { std::cout << "TTF_Init() Failed: " << TTF_GetError() << endl; } const char* fontPath = "..\\..\\data\\font\\font2.ttf"; TTF_Font* font = TTF_OpenFont(fontPath, 18); if(font == NULL) { std::cout << "Error while loading font: " << TTF_GetError() << endl; } SDL_Color c = { 255,255,255 }; const char* testtext = "This is a test text"; SDL_Surface* text = TTF_RenderText_Solid(font,testtext,c); // Access Violation here if(text == NULL) // So it's not even able to come here { std::cout << "Error while rendering: " << TTF_GetError() << endl; } 
Should work right? Well, I get an Access Violation, the 005 one, which basically tells me something I'm pointing to with a pointer isn't really there right?
Oh, the font does exist. It doesn't give me an error in that error check, and if I fill in some gibberish as path ("ASDSAD.ttf"), it does give me the error.

I've tried searching the web, but after a day, I've given up, and decided to post here..
Could anyone help me / point me in the right direction?

Share on other sites
Have you initialized SDL?

Share on other sites
Yes, everything else I've done so far works, no need to ask if I initialized SDL itself or anything, it's just that part that doesn't work.

Share on other sites
Have you pinpointed the exact line using your debugger?

Share on other sites
I plugged your code in my own program and it worked for me. The only thing that I can not verify is your font location.

Share on other sites
I discovered it was a wrong version.. I'm using SDL 1.3, with SDL_ttf 2.0.10.
I've already tried compiling the latest version (got them from the hg repo's) of both, but now it just won't open the file..

Is there any easy way to get SDL1.3 working with SDL_ttf?

Share on other sites
You might try recompiling the lib, but here's what works for me:
1. Use TTF_RenderText_Blended
2. Before freeing the surface increase refcount at it's format
Issue is, when freeing surface it leaves dead pointer at format stack.

Example:

 // load font, create text and colour here SDL_Surface* sptr = TTF_RenderText_Blended(font, text, colour); // make a texture, blit it, whatever sptr->format->refcount ++; // or assign it a 2, this is a safe pointer, we're telling not to clear the format, for it's used later by SDL SDL_FreeSurface(sptr); // no crashes should occur 

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 10
• 12
• 20
• 12
• 13
• Forum Statistics

• Total Topics
632148
• Total Posts
3004450

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!