I pinpointed segmentation fault - What is wrong?

The code is listed below. The intention is to copy some text into a rectangle. I am trying to parallel a tutorial at:
http://www.aaroncox.net/tutorials/2dtutorials/sdl_text.pdf
I traced down the point where the segmentation fault occurs - It is marked by the line of asterisks.

Can anyone see why it is getting segmentation fault?

TIA Bill S.

Code:

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <string.h>

int main( int argc, char* args[] ){

SDL_Init(SDL_INIT_VIDEO);
SDL_DisplayMode vmode;
SDL_GetDisplayMode(0, 0, &vmode);
SDL_Window *win1 = NULL;
win1 = SDL_CreateWindow("", 0, 0, vmode.w, vmode.h, SDL_WINDOW_SHOWN);

SDL_Renderer* ren1 = SDL_CreateRenderer(win1, -1, 0);
SDL_SetRenderDrawColor(ren1, 255, 255, 255, 255);
SDL_RenderClear(ren1); // fill the scene with white

SDL_Rect rect1;
rect1.x = 50;
rect1.y = 50;
rect1.w = 200;
rect1.h = 32;

SDL_Surface *surf1 = NULL;
surf1 = SDL_CreateRGBSurface(0,200,32,32,0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);

SDL_RenderDrawRect(ren1, &rect1);
SDL_SetRenderDrawColor(ren1, 255, 0, 0, 255); // the rect color (solid red)
SDL_RenderFillRect(ren1, &rect1);

TTF_Init();
TTF_Font* font = TTF_OpenFont(“ARIAL.TTF”, 12);
SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Color backgroundColor = { 0, 0, 255 };
/************************************** Segmentation Fault next line *************************************/
SDL_Surface
textSurface = TTF_RenderText_Shaded(font, “This is my text.”, foregroundColor, backgroundColor);
SDL_Rect textLocation = { 0, 0, 200, 32 };
SDL_BlitSurface(textSurface, &rect1, surf1, &textLocation);
//SDL_BlitSurface(textSurface, NULL, surf1, &textLocation);
SDL_RenderPresent(ren1); // copy to screen

SDL_Delay(5000);
SDL_FreeSurface(textSurface);
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyWindow(win1);
SDL_DestroyRenderer(ren1);
SDL_Quit();
return 0;
}

Hi,

I?d be willing to bet that it is an invalid memory access issue ? you should verify that the font pointer is not NULL. Is it? The most common reason for TTF_OpenFont to fail is because it was not able to find the font at the path specified ? in your case, does ?ARIAL.ttf? exist in the working directory of the executable that you run? If it doesn?t, that could be why the font pointer comes up NULL ? If the font doesn?t exist, simply changing the path to point to wherever that font is on your system should do the trick.

Cheers,
Jeffrey Carpenter <@Jeffrey_Carpenter>

-------------- next part --------------
A non-text attachment was scrubbed…
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1572 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20150413/e2017076/attachment.bin

Check to see if font == NULL. If it is, then the required font file isn’t loaded (dont forget it wont pick it from the systems pool of fonts - you need to supply the ttf file yourself).

MrTAToad wrote:

Check to see if font == NULL. If it is, then the required font file isn’t loaded (dont forget it wont pick it from the systems pool of fonts - you need to supply the ttf file yourself).

Thank you. Yes, that was the problem. Where is the ‘the systems pool of fonts’? Is that on my system’s PATH? How can something like this be made portable to other computers?

Your systems pool of fonts could be anywhere. To make it portable, you need to download the arial font from somewhere and put in either with the programs executable, or for a Mac, in the Resource directory.