# What is the problem here?

**URL:** <https://discourse.libsdl.org/t/what-is-the-problem-here/8547>\
**Category:** SDL Development\
**Created:** [April 9, 2003, 5:35am UTC](https://discourse.libsdl.org/t/what-is-the-problem-here/8547 "2003-04-09T05:35:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eduardo\_B\_Fonseca](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@Eduardo\_B\_Fonseca](https://discourse.libsdl.org/u/Eduardo_B_Fonseca)\
**Post date:** [April 9, 2003, 5:35am UTC](https://discourse.libsdl.org/t/what-is-the-problem-here/8547/1 "2003-04-09T05:35:01Z")

</div>

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](http://aedsolucoes.com.br)  
09:31:20 up 1 day, 22:31, 1 user, load average: 0.26, 0.38, 0.35

---

<div class="post-metadata">

**Author:** ![Pontus\_Pihlgren](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@Pontus\_Pihlgren](https://discourse.libsdl.org/u/Pontus_Pihlgren)\
**Post date:** [April 9, 2003, 6:06am UTC](https://discourse.libsdl.org/t/what-is-the-problem-here/8547/2 "2003-04-09T06:06:02Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![rsteinke\_at\_w-link.n](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@rsteinke\_at\_w-link.n](https://discourse.libsdl.org/u/rsteinke_at_w-link.n)\
**Post date:** [April 9, 2003, 9:36am UTC](https://discourse.libsdl.org/t/what-is-the-problem-here/8547/3 "2003-04-09T09:36:01Z")

</div>

> 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”

---

<div class="post-metadata">

**Author:** ![Eduardo\_B\_Fonseca](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@Eduardo\_B\_Fonseca](https://discourse.libsdl.org/u/Eduardo_B_Fonseca)\
**Post date:** [April 9, 2003, 12:14pm UTC](https://discourse.libsdl.org/t/what-is-the-problem-here/8547/4 "2003-04-09T12:14:00Z")

</div>

Thanks a lot Pontus and Ron…

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

Thanks again!

Eduardo.On Wednesday 09 April 2003 13:35, rsteinke at [w-link.net](http://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://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](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
