What is the problem here?

Hello Guys,

void draw(SDL_Surface *dst)
{
SDL_Surface *final = SDL_CreateRGBSurface(…);

(blit, blit, blit)

SDL_Surface *temp = dst;
dst = final;
SDL_FreeSurface(temp);
}

when I try to use the “dst” surface, the program segfaults (SDL parachute and
etc…)

Where is the problem here? (honestly, I can’t see it :()

Thanks!–
Eduardo B. Fonseca
ebf at aedsolucoes.com.br
09:31:20 up 1 day, 22:31, 1 user, load average: 0.26, 0.38, 0.35

void draw(SDL_Surface *dst)
{
SDL_Surface *final = SDL_CreateRGBSurface(…);

(blit, blit, blit)

SDL_Surface *temp = dst;
dst = final;
SDL_FreeSurface(temp);
}

if you do this:

SDL_Surface *someSurface = SDL_CreateRGBSurface( … );
draw(someSurface);
SDL_Blit(someSurface);

the pointer “someSurface” will be free’d since you “free(temp)” and that is the
same thing as freeing the original value of “dst”(the value passed to “draw”)
which is the value in “someSurface” that you later try to use.

You could fix it by doing:

void draw(SDL_Surface **dst)
{
SDL_Surface *final = SDL_CreateRGBSurface(…);

(blit, blit, blit)

SDL_Surface *temp = *dst;
*dst = final;
SDL_FreeSurface(temp);
}

and then:

SDL_Surface *someSurface = SDL_CreateRGBSurface( … );
draw(&someSurface);
SDL_Blit(someSurface);

This will delete the old surface and set “someSurface” to point to a new surface.

/Pontus

Hello Guys,

void draw(SDL_Surface *dst)
{
SDL_Surface *final = SDL_CreateRGBSurface(…);

(blit, blit, blit)

SDL_Surface *temp = dst;
dst = final;
SDL_FreeSurface(temp);
}

when I try to use the “dst” surface, the program segfaults (SDL parachute and
etc…)

Where is the problem here? (honestly, I can’t see it :()

You’re assigning to a temporary. You need to do something like

void draw(SDL_Surface **dst)
{

*dst = final;


}

and pass a pointer to a SDL_Surface* when you call this function.
This way, the function can set your SDL_Surface*, instead of just
setting its local copy.

Ron Steinke

“The sound of gunfire, off in the distance. I’m getting used to it now.”
– Talking Heads> From: “Eduardo B. Fonseca”

Thanks a lot Pontus and Ron…

I can spot the problem now… Thats what happens when we code at 5 am :slight_smile:

Thanks again!

Eduardo.On Wednesday 09 April 2003 13:35, rsteinke at w-link.net wrote:

From: “Eduardo B. Fonseca” <@Eduardo_B_Fonseca>

Hello Guys,

void draw(SDL_Surface *dst)
{
SDL_Surface *final = SDL_CreateRGBSurface(…);

(blit, blit, blit)

SDL_Surface *temp = dst;
dst = final;
SDL_FreeSurface(temp);
}

when I try to use the “dst” surface, the program segfaults (SDL parachute
and etc…)

Where is the problem here? (honestly, I can’t see it :()

You’re assigning to a temporary. You need to do something like

void draw(SDL_Surface **dst)
{

*dst = final;


}

and pass a pointer to a SDL_Surface* when you call this function.
This way, the function can set your SDL_Surface*, instead of just
setting its local copy.

Ron Steinke

“The sound of gunfire, off in the distance. I’m getting used to it now.”
– Talking Heads


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Eduardo B. Fonseca
ebf at aedsolucoes.com.br
16:12:19 up 2 days, 5:12, 1 user, load average: 1.21, 0.57, 0.39