Leak / Total Memory Ammount on Startup / Advice?

Hello List

I’m working on a cross-platform solution with SDL, SDL_ttf and C (as ansi as
possible). I think (valgrind and devpartner told me something about it -
precisely on creatergbsurface - what lead me to look after any surfaces not
freed. Cant find any…) I have a leak in my code, on the application exit.
But my main problem now is the amount of ram to sdl_init and
sdl_setvideomode. Each one take more then 2Mb each. Since this application
will run in embedded hardware… there is something I can do to reduce this
values? The total memory usage from the application is around 7Mb, and I
still have a lot of memory to use in another stuff. The total memory set is
8mb (and that’s huge for some systems) so… I have a big problem. I have
searched google and the list around that, but nothing fit my parameters.

There is something on MS Windows. If one minimize the application and then
restore, the application memory usage low down to 1.4, 1.5mb. Why is that?
Something about windows (kind of go to pagefile or something)? Can I
reproduce that from code (have already searched for a way to minimize the
application on startup, but found just platform specific options to do
so…)?

If anyone can point any leaks or bad practice, I’ll be glad.

SDL 1.2 static linked;
SDL_ttf 2.0.9 static linked;
Freetype 2.3.7 static linked;

Using GCC sorry I don’t know the version right now and cant have my hands on
a putty or the machine, with the same sdl/ttf/freetype versions (downloaded
all at the same time, that is why I remember that). But gcc was downloaded
this year, about 3 months ago (when I have installed the entire sysop - by
the way, gentoo 2008), the last version. Should not be old. Valgrind too,
same stuff… Don’t remember, blah, blah, blah…

&&

MS Visual Studio 2005 Pro with DevPartner, XP SP2.

Comment out all the ttf and sqlite code doesn’t low down the memory usage in
more then 300k.

If there are any other useful information that I’m missing here, please,
tell me. I’m here to make this task as easy as possible. This problems are
diving-me crazy.

Here is the code snipped :slight_smile:

/* kinda header /
/
Sdl_ttf */
typedef struct
{
TTF_Font font; / The font handler */
const char fontname; / … */
} sdl_truetype_font;

	/*  Sdl */

typedef struct
{
Uint32 rgb_map;

	sdl_truetype_font *sdl_ttf;

	SDL_Color colors[NUM_COLORS]; /* Set the number of colors to

use */
SDL_Color green; / the green of your eyes */
SDL_Color black; / the black of your soul */

	SDL_Rect dstrect; /* the main black screen coordinates */

	SDL_Event event; /* Event handler. this guy do the magic of

get user input. Whatever Kind */

	SDL_Surface *sfc; /* Where the main black screen is draw'ed

*/
} simple_direct_media_layer;

/* Glue all */
typedef struct
{
simple_direct_media_layer *sdl;
//there are other stuff here, thats why. But is a ton of
lines, so, lets keep that as short as possible.
} all_structs;

/eof header/

/* on main */

	if (( glue = malloc( sizeof(all_structs) ) ) == NULL) 
		{
			printf("ERROR ALLOCATING All Structs.\n");
			return EXIT_FAILURE;
		}
	else
		printf("Glue init ok...\n");

	if(init_sdl(glue)==EXIT_FAILURE)
		{
			error=1;
			printf("ERROR Calling Sdl Struct

constructor.\n");
goto free_glue;
}
else
printf(“Sdl init ok…\n”);
/* eof on main */

int init_sdl(all_structs *glue)
{
int i, rdiff, bdiff, gdiff;

	printf("\nStarting SDL initialization...\n");

	if (( glue->sdl = malloc( sizeof(simple_direct_media_layer)

) ) == NULL)
{
printf(“ERROR ALLOCATING Simple Direct Media
Layer\n”);
return EXIT_FAILURE;
}
else
printf(" Sdl Struct allocated.\n");

	if (( glue->sdl->sdl_ttf = malloc( sizeof(sdl_truetype_font)

) ) == NULL)
{
printf(“ERROR ALLOCATING Sdl Truetype
Font\n”);
goto free_sdl;
}
else
printf(" Sdl_ttf Struct allocated.\n");

	if (( glue->sdl->black = malloc( sizeof(SDL_Color) ) ) ==

NULL)
{
printf(“ERROR ALLOCATING background
color\n”);
goto free_sdl_ttf;
}
else
printf(" Sdl Color Black (Background) Struct
allocated.\n");

	if (( glue->sdl->green = malloc( sizeof(SDL_Color) ) ) ==

NULL)
{
printf(“ERROR ALLOCATING foreground
color\n”);
goto free_black;
}
else
printf(" Sdl Color Green (Font color) Struct
allocated.\n");

	glue->sdl->black->b = 0x00;
	glue->sdl->black->g = 0x00;
	glue->sdl->black->r = 0x00;

	glue->sdl->green->b = 0;
	glue->sdl->green->g = 255;
	glue->sdl->green->r = 0;

	glue->sdl->dstrect.x=(Sint16)0;
	glue->sdl->dstrect.y=(Sint16)0;
	glue->sdl->dstrect.w=(Sint16)0;
	glue->sdl->dstrect.h=(Sint16)0;

	rdiff = glue->sdl->black->r - glue->sdl->green->r;
	gdiff = glue->sdl->black->g - glue->sdl->green->g;
	bdiff = glue->sdl->black->b - glue->sdl->green->b;
	for ( i=0; i<NUM_COLORS; i++ ) 
		{
			glue->sdl->colors[i].r = glue->sdl->green->r
  • (Uint8)(i*rdiff)/4;
    glue->sdl->colors[i].g = glue->sdl->green->g
  • (Uint8)(i*gdiff)/4;
    glue->sdl->colors[i].b = glue->sdl->green->b
  • (Uint8)(ibdiff)/4;
    }
    /
    The first beast… From 1.325 to 3.7 right after SDL_Init. */
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
    {
    printf(“Couldn’t initialize SDL: %s\n”,
    SDL_GetError());
    goto free_green;
    }
    else
    printf(" Sdl Started.\n");

/* Now, on SetVideoMode, the memory grow more then 2mb…, total memory
usage 6.498 :frowning: */

	if ((glue->sdl->sfc = SDL_SetVideoMode(WIDTH, HEIGHT, BITS,

SDL_NOFRAME)) == NULL) //SDL_NOFRAME | SDL_SWSURFACE | SDL_DOUBLEBUF |
SDL_ASYNCBLIT
{
printf(“Couldn’t set %ix%ix%i video mode:
%s\n”, WIDTH, HEIGHT, BITS, SDL_GetError());
goto sdl_quit;
}
else
printf(" Surface sizes set.\n");

	SDL_SetColors(glue->sdl->sfc, glue->sdl->colors, 0,

NUM_COLORS);

	if(SDL_FillRect(glue->sdl->sfc, NULL,

SDL_MapRGB(glue->sdl->sfc->format, glue->sdl->black->r, glue->sdl->black->g,
glue->sdl->black->b))<0)
{
printf(“Couldn’t fill the rect with
black.\n”);
goto sdl_quit;
}
else
printf(" Window - About to open.\n");

/**/

	if ( TTF_Init() < 0 ) 
		{
			printf("Couldn't initialize TTF:

%s\n",SDL_GetError());
goto sdl_quit;
}
else
printf(" TTF Started.\n");

	if ((glue->sdl->sdl_ttf->font = TTF_OpenFont(FONTNAME,

DEFAULT_PTSIZE)) == NULL)
{
printf(“Couldn’t load %d pt font from %s:
%s\n”, DEFAULT_PTSIZE, FONTNAME, SDL_GetError());
goto clean_all;
}
else
printf(" Font set.\n");

	TTF_SetFontStyle(glue->sdl->sdl_ttf->font,

TTF_STYLE_NORMAL);

/**/ 
	SDL_FreeSurface(glue->sdl->sfc);

	SDL_Flip(glue->sdl->sfc);
	//SDL_UpdateRects(glue->sdl->sfc, 1, &(glue->sdl->dstrect));
	SDL_ShowCursor(SDL_DISABLE);

	printf("End of SDL Initialization.\n\n");
	return EXIT_SUCCESS;

clean_all:
TTF_CloseFont(glue->sdl->sdl_ttf->font);
TTF_Quit();

sdl_quit:
SDL_FreeSurface(glue->sdl->sfc);
SDL_Quit();

free_green:
free(glue->sdl->green);

free_black:
free(glue->sdl->black);

free_sdl_ttf:
free(glue->sdl->sdl_ttf);

free_sdl:
free(glue->sdl);

	return EXIT_FAILURE;
}

/* That’s how Im writting to the screen (kinda) */

int switch_pages(all_structs *glue, int page)
{
int rc, i;

	rc=0;
	i=0;

	rc = SDL_FillRect(glue->sdl->sfc, NULL,

SDL_MapRGB(glue->sdl->sfc->format, glue->sdl->black->r, glue->sdl->black->g,
glue->sdl->black->b));

	printf("Loading page index %i.\n",page);

	sprintf(glue->sqlite->sql_mod,"select id, value from line

where id_screen = %i order by id;",page);
rc = sqlite3_prepare(glue->sqlite->db, glue->sqlite->sql,
-1, &(glue->sqlite->stmt), 0);
if(rc!=SQLITE_OK)
{
printf(“sql error #%d: %s\n”, rc,
sqlite3_errmsg(glue->sqlite->db));
return EXIT_FAILURE;
}
else
{
while((rc =
sqlite3_step(glue->sqlite->stmt)) != SQLITE_DONE)
{
switch(rc)
{
case
SQLITE_ROW:
{

sprintf(glue->ld->text,"%s",sqlite3_column_text(glue->sqlite->stmt,1));

if(i<1)

glue->ld->j = 4;

i++;

glue->ld->row = TTF_RenderText_Solid(glue->sdl->sdl_ttf->font,
glue->ld->text, *(glue->sdl->green));

if ( glue->ld->row != NULL )

{

glue->sdl->dstrect.x = 4;

glue->sdl->dstrect.w = (Uint16)glue->ld->row->w;

glue->sdl->dstrect.h = (Uint16)glue->ld->row->h;

glue->sdl->dstrect.y = (Uint16)glue->ld->j;

glue->ld->j = glue->ld->j + (int)glue->ld->row->h + 4;

SDL_BlitSurface(glue->ld->row, NULL, glue->sdl->sfc, &(glue->sdl->dstrect));

SDL_FreeSurface(glue->ld->row);

}
}

break;
}
}

sqlite3_finalize(glue->sqlite->stmt);
glue->sqlite->stmt = 0;
	SDL_FreeSurface(glue->sdl->sfc);
	SDL_UpdateRect(glue->sdl->sfc, 0, 0, 0, 0);
	SDL_ShowCursor(SDL_DISABLE);

	return EXIT_SUCCESS;
}

/* Initialize Key Event Capture */

int key_loop(all_structs *glue)//simple_direct_media_layer *sdl)
{
int done, page, mpage, minpage;
done=0;
page=1;
mpage=22;
minpage=1;
while (!done)
{
SDL_Delay(10);
while(SDL_PollEvent(&(glue->sdl->event)))
{

switch(glue->sdl->event.type)
{
case
SDL_QUIT:
done
= 1;

break;
case
SDL_KEYDOWN:

switch(glue->sdl->event.key.keysym.sym)

{

case SDLK_ESCAPE:

done = 1;

break;

case SDLK_F2:

if (page==minpage)

page=mpage;

else

page–;

if(switch_pages(glue,page)==EXIT_FAILURE)

done = 2;

break;

case SDLK_F3:

if (page==mpage)

page=minpage;

else

page++;

if(switch_pages(glue,page)==EXIT_FAILURE)

done = 2;

break;

default:

break;

}
}
}
}
if(done>1)
return EXIT_FAILURE;

	return EXIT_SUCCESS;
}

Another point of my interest is - can I run sdl graphics on pure MSDOS
environments (I really don’t have one to install here and give it a try)?
Like DOS 6.0? Cant find anything useful on gloogle except some stuff to play
music.

Thanks
Rafael

Hello List

I’m working on a cross-platform solution with SDL, SDL_ttf and C (as ansi as
possible). I think (valgrind and devpartner told me something about it -
precisely on creatergbsurface - what lead me to look after any surfaces not
freed. Cant find any…) I have a leak in my code, on the application exit.
But my main problem now is the amount of ram to sdl_init and
sdl_setvideomode. Each one take more then 2Mb each. Since this application
will run in embedded hardware… there is something I can do to reduce this
values? The total memory usage from the application is around 7Mb, and I
still have a lot of memory to use in another stuff. The total memory set is
8mb (and that’s huge for some systems) so… I have a big problem. I have
searched google and the list around that, but nothing fit my parameters.

There is something on MS Windows. If one minimize the application and then
restore, the application memory usage low down to 1.4, 1.5mb. Why is that?
Something about windows (kind of go to pagefile or something)? Can I
reproduce that from code (have already searched for a way to minimize the
application on startup, but found just platform specific options to do
so…)?

If anyone can point any leaks or bad practice, I’ll be glad.

SDL 1.2 static linked;
SDL_ttf 2.0.9 static linked;
Freetype 2.3.7 static linked;

Using GCC sorry I don’t know the version right now and cant have my hands on
a putty or the machine, with the same sdl/ttf/freetype versions (downloaded
all at the same time, that is why I remember that). But gcc was downloaded
this year, about 3 months ago (when I have installed the entire sysop - by
the way, gentoo 2008), the last version. Should not be old. Valgrind too,
same stuff… Don’t remember, blah, blah, blah…

&&

MS Visual Studio 2005 Pro with DevPartner, XP SP2.

Comment out all the ttf and sqlite code doesn’t low down the memory usage in
more then 300k.

If there are any other useful information that I’m missing here, please,
tell me. I’m here to make this task as easy as possible. This problems are
diving-me crazy.

Here is the code snipped :slight_smile:

/* kinda header /
/
Sdl_ttf */
typedef struct
{
TTF_Font font; / The font handler */
const char fontname; / … */
} sdl_truetype_font;

	/*  Sdl */

typedef struct
{
Uint32 rgb_map;

	sdl_truetype_font *sdl_ttf;

	SDL_Color colors[NUM_COLORS]; /* Set the number of colors to

use */
SDL_Color green; / the green of your eyes */
SDL_Color black; / the black of your soul */

	SDL_Rect dstrect; /* the main black screen coordinates */

	SDL_Event event; /* Event handler. this guy do the magic of

get user input. Whatever Kind */

	SDL_Surface *sfc; /* Where the main black screen is draw'ed

*/
} simple_direct_media_layer;

/* Glue all */
typedef struct
{
simple_direct_media_layer *sdl;
//there are other stuff here, thats why. But is a ton of
lines, so, lets keep that as short as possible.
} all_structs;

/eof header/

/* on main */

	if (( glue = malloc( sizeof(all_structs) ) ) == NULL) 
		{
			printf("ERROR ALLOCATING All Structs.\n");
			return EXIT_FAILURE;
		}
	else
		printf("Glue init ok...\n");

	if(init_sdl(glue)==EXIT_FAILURE)
		{
			error=1;
			printf("ERROR Calling Sdl Struct

constructor.\n");
goto free_glue;
}
else
printf(“Sdl init ok…\n”);
/* eof on main */

int init_sdl(all_structs *glue)
{
int i, rdiff, bdiff, gdiff;

	printf("\nStarting SDL initialization...\n");

	if (( glue->sdl = malloc( sizeof(simple_direct_media_layer)

) ) == NULL)
{
printf(“ERROR ALLOCATING Simple Direct Media
Layer\n”);
return EXIT_FAILURE;
}
else
printf(" Sdl Struct allocated.\n");

	if (( glue->sdl->sdl_ttf = malloc( sizeof(sdl_truetype_font)

) ) == NULL)
{
printf(“ERROR ALLOCATING Sdl Truetype
Font\n”);
goto free_sdl;
}
else
printf(" Sdl_ttf Struct allocated.\n");

	if (( glue->sdl->black = malloc( sizeof(SDL_Color) ) ) ==

NULL)
{
printf(“ERROR ALLOCATING background
color\n”);
goto free_sdl_ttf;
}
else
printf(" Sdl Color Black (Background) Struct
allocated.\n");

	if (( glue->sdl->green = malloc( sizeof(SDL_Color) ) ) ==

NULL)
{
printf(“ERROR ALLOCATING foreground
color\n”);
goto free_black;
}
else
printf(" Sdl Color Green (Font color) Struct
allocated.\n");

	glue->sdl->black->b = 0x00;
	glue->sdl->black->g = 0x00;
	glue->sdl->black->r = 0x00;

	glue->sdl->green->b = 0;
	glue->sdl->green->g = 255;
	glue->sdl->green->r = 0;

	glue->sdl->dstrect.x=(Sint16)0;
	glue->sdl->dstrect.y=(Sint16)0;
	glue->sdl->dstrect.w=(Sint16)0;
	glue->sdl->dstrect.h=(Sint16)0;

	rdiff = glue->sdl->black->r - glue->sdl->green->r;
	gdiff = glue->sdl->black->g - glue->sdl->green->g;
	bdiff = glue->sdl->black->b - glue->sdl->green->b;
	for ( i=0; i<NUM_COLORS; i++ ) 
		{
			glue->sdl->colors[i].r = glue->sdl->green->r
  • (Uint8)(i*rdiff)/4;
    glue->sdl->colors[i].g = glue->sdl->green->g
  • (Uint8)(i*gdiff)/4;
    glue->sdl->colors[i].b = glue->sdl->green->b
  • (Uint8)(ibdiff)/4;
    }
    /
    The first beast… From 1.325 to 3.7 right after SDL_Init. */
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
    {
    printf(“Couldn’t initialize SDL: %s\n”,
    SDL_GetError());
    goto free_green;
    }
    else
    printf(" Sdl Started.\n");

/* Now, on SetVideoMode, the memory grow more then 2mb…, total memory
usage 6.498 :frowning: */

	if ((glue->sdl->sfc = SDL_SetVideoMode(WIDTH, HEIGHT, BITS,

SDL_NOFRAME)) == NULL) //SDL_NOFRAME | SDL_SWSURFACE | SDL_DOUBLEBUF |
SDL_ASYNCBLIT
{
printf(“Couldn’t set %ix%ix%i video mode:
%s\n”, WIDTH, HEIGHT, BITS, SDL_GetError());
goto sdl_quit;
}
else
printf(" Surface sizes set.\n");

	SDL_SetColors(glue->sdl->sfc, glue->sdl->colors, 0,

NUM_COLORS);

	if(SDL_FillRect(glue->sdl->sfc, NULL,

SDL_MapRGB(glue->sdl->sfc->format, glue->sdl->black->r, glue->sdl->black->g,
glue->sdl->black->b))<0)
{
printf(“Couldn’t fill the rect with
black.\n”);
goto sdl_quit;
}
else
printf(" Window - About to open.\n");

/**/

	if ( TTF_Init() < 0 ) 
		{
			printf("Couldn't initialize TTF:

%s\n",SDL_GetError());
goto sdl_quit;
}
else
printf(" TTF Started.\n");

	if ((glue->sdl->sdl_ttf->font = TTF_OpenFont(FONTNAME,

DEFAULT_PTSIZE)) == NULL)
{
printf(“Couldn’t load %d pt font from %s:
%s\n”, DEFAULT_PTSIZE, FONTNAME, SDL_GetError());
goto clean_all;
}
else
printf(" Font set.\n");

	TTF_SetFontStyle(glue->sdl->sdl_ttf->font,

TTF_STYLE_NORMAL);

/**/ 
	SDL_FreeSurface(glue->sdl->sfc);

	SDL_Flip(glue->sdl->sfc);
	//SDL_UpdateRects(glue->sdl->sfc, 1, &(glue->sdl->dstrect));
	SDL_ShowCursor(SDL_DISABLE);

	printf("End of SDL Initialization.\n\n");
	return EXIT_SUCCESS;

clean_all:
TTF_CloseFont(glue->sdl->sdl_ttf->font);
TTF_Quit();

sdl_quit:
SDL_FreeSurface(glue->sdl->sfc);
SDL_Quit();

free_green:
free(glue->sdl->green);

free_black:
free(glue->sdl->black);

free_sdl_ttf:
free(glue->sdl->sdl_ttf);

free_sdl:
free(glue->sdl);

	return EXIT_FAILURE;
}

/* That’s how Im writting to the screen (kinda) */

int switch_pages(all_structs *glue, int page)
{
int rc, i;

	rc=0;
	i=0;

	rc = SDL_FillRect(glue->sdl->sfc, NULL,

SDL_MapRGB(glue->sdl->sfc->format, glue->sdl->black->r, glue->sdl->black->g,
glue->sdl->black->b));

	printf("Loading page index %i.\n",page);

	sprintf(glue->sqlite->sql_mod,"select id, value from line

where id_screen = %i order by id;",page);
rc = sqlite3_prepare(glue->sqlite->db, glue->sqlite->sql,
-1, &(glue->sqlite->stmt), 0);
if(rc!=SQLITE_OK)
{
printf(“sql error #%d: %s\n”, rc,
sqlite3_errmsg(glue->sqlite->db));
return EXIT_FAILURE;
}
else
{
while((rc =
sqlite3_step(glue->sqlite->stmt)) != SQLITE_DONE)
{
switch(rc)
{
case
SQLITE_ROW:
{

sprintf(glue->ld->text,"%s",sqlite3_column_text(glue->sqlite->stmt,1));

if(i<1)

glue->ld->j = 4;

i++;

glue->ld->row = TTF_RenderText_Solid(glue->sdl->sdl_ttf->font,
glue->ld->text, *(glue->sdl->green));

if ( glue->ld->row != NULL )

{

glue->sdl->dstrect.x = 4;

glue->sdl->dstrect.w = (Uint16)glue->ld->row->w;

glue->sdl->dstrect.h = (Uint16)glue->ld->row->h;

glue->sdl->dstrect.y = (Uint16)glue->ld->j;

glue->ld->j = glue->ld->j + (int)glue->ld->row->h + 4;

SDL_BlitSurface(glue->ld->row, NULL, glue->sdl->sfc, &(glue->sdl->dstrect));

SDL_FreeSurface(glue->ld->row);

}
}

break;
}
}

sqlite3_finalize(glue->sqlite->stmt);
glue->sqlite->stmt = 0;
	SDL_FreeSurface(glue->sdl->sfc);
	SDL_UpdateRect(glue->sdl->sfc, 0, 0, 0, 0);
	SDL_ShowCursor(SDL_DISABLE);

	return EXIT_SUCCESS;
}

/* Initialize Key Event Capture */

int key_loop(all_structs *glue)//simple_direct_media_layer *sdl)
{
int done, page, mpage, minpage;
done=0;
page=1;
mpage=22;
minpage=1;
while (!done)
{
SDL_Delay(10);
while(SDL_PollEvent(&(glue->sdl->event)))
{

switch(glue->sdl->event.type)
{
case
SDL_QUIT:
done
= 1;

break;
case
SDL_KEYDOWN:

switch(glue->sdl->event.key.keysym.sym)

{

case SDLK_ESCAPE:

done = 1;

break;

case SDLK_F2:

if (page==minpage)

page=mpage;

else

page–;

if(switch_pages(glue,page)==EXIT_FAILURE)

done = 2;

break;

case SDLK_F3:

if (page==mpage)

page=minpage;

else

page++;

if(switch_pages(glue,page)==EXIT_FAILURE)

done = 2;

break;

default:

break;

}
}
}
}
if(done>1)
return EXIT_FAILURE;

	return EXIT_SUCCESS;
}

Another point of my interest is - can I run sdl graphics on pure MSDOS
environments (I really don’t have one to install here and give it a try)?
Like DOS 6.0? Cant find anything useful on gloogle except some stuff to play
music.

That was the 8 time i try to post this here. Last try, for sure.

Thanks
Rafael