Segmentation fault ?? Where my error checker ??

i started getting Segmentation fault trying to understand why.
i tested it down to my drawtext function in my graphic core but i don't see what i'm doing wrong.

graphic core
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "graphiccore.h"
namespace Magi
{		
SDL_Surface *Image ( std::string file ) {
	SDL_Surface *temp = NULL , *image = NULL;
	temp = IMG_Load ( file.c_str() );
	if ( temp != NULL ) {
		image = SDL_DisplayFormat ( temp );
		SDL_FreeSurface ( temp );
	}	
	return image;
}
void Graph::Clean()
{
	if ( gGraph.size() > 0 )
	{
		std::vector<gImage>::iterator it;
		for (it = gGraph.begin(); it < gGraph.end(); it++ ) 
		{
			SDL_FreeSurface( (*it).Img );
		}
	}
}
	
void Graph::Add( std::string img_name , std::string id )
{
	if ( gMap[id] == 0 )
	{
		gImage tmp;
		tmp.Img = Image( img_name );
		gGraph.push_back( tmp );
		gMap[id] = gGraph.size();
	}
}
void Graph::Draw( std::string id , SDL_Surface *canvas , SDL_Rect rRect )
{
	int idd;
	idd = gMap[id];
	if (idd > 0)
	  SDL_BlitSurface( gGraph[idd-1].Img , NULL , canvas , &rRect );
}
// adding text controls

void Text::Clean()
{
	if ( tFont.size() > 0 )
	{
		std::vector<fFont>::iterator it;
		for (it = tFont.begin(); it < tFont.end(); it++ ) 
		{
			TTF_CloseFont( (*it).font );
		}
	}
	if ( tText.size() > 0 )
	{
		std::vector<gImage>::iterator itt;
		for (itt = tText.begin(); itt < tText.end(); itt++ ) 
		{
			SDL_FreeSurface( (*itt).Img );
		}
	}
}
void Text::AddFont( std::string Font , int fontsize ,std::string id )
{
	if (fMap[id] == 0)
	{
		fFont tmp;
		tmp.font = TTF_OpenFont( Font.c_str() , fontsize );
		if (tmp.font == NULL) { printf(" Null font"); }
		tFont.push_back( tmp );
		fMap[id] = tFont.size();
		TTF_CloseFont( tmp.font );
	}
}

void Text::SetFont( std::string Fontid )
{
	if (fMap[Fontid] > 0)
	{
		currentfont = Fontid;
	}
}
	
void Text::AddText( std::string text , std::string id , std::string fontid )
{
	SDL_Color fcolor = { 255 ,255 ,255 };
	if ( tMap[id] == 0 )
	{
		int idd;
		idd = fMap[fontid]; 
		gImage tmp;
		if ( idd > 0 )
		{
			tmp.Img = TTF_RenderText_Solid( tFont[idd-1].font , text.c_str() , fcolor );
		}
		else
		{
			idd = fMap[currentfont];
			tmp.Img = TTF_RenderText_Solid( tFont[idd-1].font , text.c_str() , fcolor );
		}
		tText.push_back( tmp );
		tMap[id] = tText.size();
		SDL_FreeSurface( tmp.Img );
	}
}

void Text::DrawText( std::string idtxt , SDL_Surface *canvas , SDL_Rect rRect )
{
	int idd , idmap;
	idd = tMap[idtxt];
	if ( idd > 0 )
	{
		SDL_BlitSurface( tText[idd-1].Img , NULL , canvas , &rRect );
	}
	else
	{
		idmap = fMap[currentfont];
		SDL_Surface *tmp = NULL;
		SDL_Color fcolor = { 255 ,255 ,255 };
		tmp = TTF_RenderText_Solid( tFont[idmap-1].font , idtxt.c_str() , fcolor );
		SDL_BlitSurface( tmp , NULL , canvas , &rRect );
		SDL_FreeSurface ( tmp );
	}
}
// added features
SDL_Rect Rect( int x, int y ) {
	SDL_Rect rect;
	rect.x = x;
	rect.y = y;
	
	return rect;
}
SDL_Rect Rect( int x, int y , int w , int h ) {
	SDL_Rect rect;
	rect.x = x;
	rect.y = y;
	rect.h = h;
	rect.w = w;
	
	return rect;
}
}


my graphic heaader
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
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "gobal.h"

#ifndef GRAPHICCORE_H
#define GRAPHICCORE_H

namespace Magi
{
struct gImage
{
	SDL_Surface *Img;
};	


struct fFont
{
	TTF_Font *font;	
};


class Graph
{
	private:
		std::vector<gImage> gGraph; 
		std::map<std::string,int> gMap;
		
	public:
		void Add( std::string img_name , std::string id );
		void Draw( std::string id  , SDL_Surface *canvas , SDL_Rect rRect );
		void Clean();

};

class Text
{
private:
	std::vector<gImage> tText;
	std::vector<fFont> tFont;
	std::map<std::string,int> tMap , fMap;
	std::string currentfont;

public:
	void AddFont( std::string Font , int fontsize ,std::string id );
	void SetFont( std::string Fontid );
	void AddText( std::string text , std::string id , std::string fontid );
	void DrawText( std::string idtxt , SDL_Surface *canvas , SDL_Rect rRect );
	void Clean();
};

SDL_Rect Rect( int x, int y );
SDL_Rect Rect( int x, int y , int w , int h );
}
#endif 

my test file
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "engine.h"

struct Globs
{
	Magi::cProcess Game;
	Magi::Text Gtext;
	Magi::Graph Gimage;
	
	SDL_Surface *screen;
	SDL_Rect rscreen;
	SDL_Event event;
	
	bool gameover;
	int tick , runtick;
	
	Globs()
	{
		gameover = false;
		tick = 0;
		runtick = 0;
	}
} glob;

// change an int to string
std::string itos( int n ) {
	std::ostringstream itos;
	itos.str("");
	itos << n;
	return itos.str();
}

void start()
{
	SDL_Init ( SDL_INIT_EVERYTHING );
	TTF_Init();
	glob.screen = SDL_SetVideoMode ( 800 , 600 , 32 , SDL_SWSURFACE );
	SDL_WM_SetCaption ( "Drake Engine Test", NULL );
	
	glob.Gtext.AddFont( "/usr/share/fonts/truetype/freefont/FreeSerif.ttf" , 16 , "Serif16" );
	glob.Gtext.SetFont( "Serif16" );
	
	//glob.Gtext.AddText( "Hello , just a test" , "Hi" , "Serif16" );
	
	//glob.Gimage.Add( "grass.bmp" , "grass" );
	glob.rscreen = Magi::Rect( 0 , 0 , 800 , 600 );
}

void gameloop( Magi::Purpose P ) {
	
	switch (P) {
		case Magi::pInit: {
			
			break;
		}
		case Magi::pStop: {
			
			break;
		}
		case Magi::pStart: {
			if ( glob.gameover == false ) {
				if (SDL_PollEvent ( &glob.event ) ) {
					switch ( glob.event.type ) {
						case SDL_QUIT:
				  			glob.gameover = true;
				  			break;
						case SDL_KEYDOWN:
				 			if ( glob.event.key.keysym.sym == SDLK_ESCAPE ||
				       		glob.event.key.keysym.sym == SDLK_q) {
				  				glob.gameover = true;
				  				break;
				  			} 
				  		break;
			  		}
		  		}
		  		//blit to screen here
		  		SDL_FillRect ( glob.screen , &glob.rscreen , 0 );
			}
			break;
		}
		case Magi::pSleep: {
			printf (" error ");
			break;
		}
		
	}
}

void fps( Magi::Purpose P ) {
	switch (P) 
	{
		case Magi::pInit: 
		{
			glob.Game.Settick ( "FPS" , 1000 , 1000 );
			break;
		}
		case Magi::pStop: 
		{
			printf ( "%d\n", glob.tick);
			break;
		}
		case Magi::pStart: 
		{ 
			glob.Game.Tick( "FPS" );
			glob.runtick++;
			std::string tex;
			
			tex = itos ( glob.tick );
			//glob.Gtext.DrawText( tex , glob.screen , Magi::Rect( 50 , 300 ) );
			
			glob.tick = 0;
			break;
		}
		
		case Magi::pSleep: 
		{
			glob.tick++;
			break;
		}
	}
}
			
// game start
int main ( int argc , char* args[] ) {
	start();
	glob.Game.Add( gameloop, "Main" );
	glob.Game.Add( fps , "FPS" );
	//game loop
	while ( !glob.gameover ) 
	{
		Magi::enginecontrol( glob.Game );
		//glob.Gimage.Draw( "grass" , glob.screen , Magi::Rect(50,50) );
		//glob.Gtext.DrawText( "Hi" , glob.screen , Magi::Rect(50,50) );
		SDL_Flip ( glob.screen ) ;	
	}
	// clean up
	printf("\n just in : ");
	glob.Game.Pop( "FPS" );
	printf (" fault here ");
	SDL_FreeSurface( glob.screen );
	glob.Gtext.Clean();
	glob.Gimage.Clean();
	printf("clean");

	TTF_Quit();
	SDL_Quit();
	return 0;

One obvious issue is that you are holding pointers in your Graph class through vector<gImage>.Img (SDL_Surface*) and in Globs yet I don't see a copy constructor, destructor or an assignment operator defined anywhere. Given that you are passing everything by value rather than const reference you are copying and destroying objects all over the place. Your problem likely lies there. In any case, I would not venture much further into debugging the problem until the basics are fixed.
I fiqured it out . I was freeing objects that i thought i had too. Pointers are somewhat confusing.
had to remove it in a couple of areas
1
2
3
4
5
6
7
8
9
10
11
12
void Text::AddFont( std::string Font , int fontsize ,std::string id )
{
	if (fMap[id] == 0)
	{
		fFont tmp;
		tmp.font = TTF_OpenFont( Font.c_str() , fontsize );
		if (tmp.font == NULL) { printf(" Null font"); }
		tFont.push_back( tmp );
		fMap[id] = tFont.size();
		//TTF_CloseFont( tmp.font ); <-- causing segmentation fault (not to be free)
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Text::AddText( std::string text , std::string id , std::string fontid )
{
	SDL_Color fcolor = { 255 ,255 ,255 };
	if ( tMap[id] == 0 )
	{
		int idd;
		idd = fMap[fontid]; 
		gImage tmp;
		if ( idd > 0 )
		{
			tmp.Img = TTF_RenderText_Solid( tFont[idd-1].font , text.c_str() , fcolor );
		}
		else
		{
			idd = fMap[currentfont];
			tmp.Img = TTF_RenderText_Solid( tFont[idd-1].font , text.c_str() , fcolor );
		}
		tText.push_back( tmp );
		tMap[id] = tText.size();
		//SDL_FreeSurface( tmp.Img ); <-- causing segmentation fault (not to be free) 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Text::DrawText( std::string idtxt , SDL_Surface *canvas , SDL_Rect rRect )
{
	int idd , idmap;
	idd = tMap[idtxt];
	if ( idd > 0 )
	{
		SDL_BlitSurface( tText[idd-1].Img , NULL , canvas , &rRect );
	}
	else
	{
		idmap = fMap[currentfont];
		SDL_Surface *tmp = NULL;
		SDL_Color fcolor = { 255 ,255 ,255 };
		tmp = TTF_RenderText_Solid( tFont[idmap-1].font , idtxt.c_str() , fcolor );
		SDL_BlitSurface( tmp , NULL , canvas , &rRect );
		//SDL_FreeSurface ( tmp ); <-- is this one needed.
	}
}

Last edited on
Topic archived. No new replies allowed.