Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL_ttf crashing at address 0x0000000

Started by Beshon May 8, 2013 at 10:16 PM 3 replies 3.1k views
Original Post
Beshon
Beshon

The error is Unhandled exception at 0x6F4C2A9D (SDL_ttf.dll) in helloWorld.exe: 0xC0000005: Access violation reading location 0x00000000

The error is coming up at line 'text = TTF_RenderText_Blended(font, "Hello World!", color3);'

I looked it up and most people are saying its something to do with trying to nullify a pointer which is already null but as far as I can tell this isn't the issue here, so any help from someone would be really appreciated.

Full code is at http://pastebin.com/LhrZEga6

Code before the error occurs is below


#include "SDL.h"
#include "SDL_ttf.h"

int main( int argc, char* args[] )
{
    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );
	TTF_Init();
	
	//Font stuff
	TTF_Font* font = TTF_OpenFont("Arial.ttf", 32);

	//Creates game screen and image object
	SDL_Surface* screen, *image, *text;
	screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

	//Creates game loop
	bool running = true;

	//Creates text
	SDL_Color color3 = {0xff, 0xff, 0xff};
        text = TTF_RenderText_Blended(font, "Hello World!", color3);

Thanks again

Servant of the Lord
Servant of the Lord

You didn't check whether 'font' loaded correctly:


if(!font)
{
     std::cout << "An error occured loading the font \"" << filepath << "\".\n"
                    << "TTF_OpenFont() says: \"" << TTF_GetError() << "\"" << std::endl;
     return 0;
}

smile.png

rip-off
rip-off

All your SDL calls should have error checking. For example, TTF_Init() could be failing too.

Beshon
Beshon

Ah thanks a bunch

It ended up being that I had to include the full directory of the font file and not just the name!

Thanks again

Servant of the Lord
Servant of the Lord
std::cout is a text stream that can be redirected wherever you like. Normally it is directed to the console, but it doesn't have to be.
SDL by default redirects it to a file called stdout.txt that is created in the same folder as your program's executable.

TTF_OpenFont() opens a .ttf file. It expects a file path to an actual file on your computer. Is there a file called "Arial.ttf" next to your executable?
If you want to get a font from the common font folder on your Windows machine, you have to provide the actual path to that folder and give the actual font name. There are some Win32-specific functions that Microsoft provides that can do that.

[rollup="Windows-specific code for getting the font folder"]
#include <windows>

std::string GetKnownFolderPath(int folderID)
{
	std::string folderPath = "[Error - Windows_GetKnownFolderPath()]";
	
	char buffer[MAX_PATH];
	HRESULT result = SHGetFolderPathA(0, folderID, 0, 0, buffer);
	if (SUCCEEDED(result))
	{
		folderPath.assign(buffer);
	}
	else
	{
		//Report the error.
		if(result == E_INVALIDARG)
		{
			std::cout << "GetKnownFolderPath(): The folderID passed to SHGetFolderPathA() was invalid." << std::endl;
		}
		else if(result == S_FALSE || result == E_FAIL)
		{
			std::cout << "GetKnownFolderPath(): The folderID passed to SHGetFolderPathA() was valid, but does not exist." << std::endl
		}
		else
		{
			std::cout << "GetKnownFolderPath(): Something went wrong with SHGetFolderPathA(), but I don't know what." << std::cout;
		}
	}
	
	return folderPath;
}

int main()
{
   std::cout << "Font folder is: " << GetKnownFolderPath(CSIDL_FONTS) << std::endl;
   return 0;
}

For other possible CSIDL_ constants, see:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762494(v=vs.85).aspx
[/rollup]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.