sdl library error loading text function

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//loads text into sdl texture
  SDL_Texture * sdlJeu::create_text_texture (const char *text ,TTF_Font *Fontsdl, SDL_Color textColor)
    {
        
    textsurface = TTF_RenderText_Solid( Fontsdl, text, textColor );
    if( textsurface == NULL )
    {
        printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
    }
    else
    {
        //Create texture from surface pixels
        mytexture = SDL_CreateTextureFromSurface( renderer, textsurface );
        if( mytexture == NULL )
        {
            printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
        }
    }
            SDL_FreeSurface( textsurface );
            return mytexture ;
    }


this function causes a segmentation error crash
can someone explain to me why
Last edited on
Using the debugger 101
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ cat baz.cpp
#include <iostream>
using namespace std;

void foo ( ) {
  int *p = 0;
  *p = 0; // boom
}

void bar ( ) {
  int a = 2;
  foo();
}

int main ( ) {
  bar();
}

# compile with -g for debug goodness
$ g++ -g baz.cpp

$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) run
Starting program: ./a.out 

Program received signal SIGSEGV, Segmentation fault.
0x00000000004006c6 in foo () at baz.cpp:6
6	  *p = 0; // boom
(gdb) bt
#0  0x00000000004006c6 in foo () at baz.cpp:6
#1  0x00000000004006e3 in bar () at baz.cpp:11
#2  0x00000000004006ef in main () at baz.cpp:15
(gdb) up
#1  0x00000000004006e3 in bar () at baz.cpp:11
11	  foo();
(gdb) print a
$1 = 2
(gdb) 


So now you compile your program with the -g flag, run it in the debugger and show us a stack trace and the value(s) of interesting variables.

Your indentation is a mess, but it looks like you can get to SDL_FreeSurface( textsurface ); with a NULL pointer.
Difficult to say.

Are you calling it from the main thread?
Is the TTF_Font object correctly initialized and is its pointer valid?
Is the SDL_Renderer object correctly initialized and is its pointer valid?
Does the const char pointer point to a valid C string in readable memory?

By the way, there's no reason for textsurface and mytexture to be class members. They should be local variables to prevent the function from causing undesirable side-effects.
Topic archived. No new replies allowed.