Sign in to follow this  

segmentation fault

This topic is 4270 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i think i have managed to determine that the cause for this error
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

is this piece of code
void SDLCharacter::draw()
{
	SDL_Surface *imagerotated=NULL;
	imagerotated=rotozoomSurface(m_image, m_currentAngle,1,0);
	m_image=imagerotated;
	m_graphics->drawsprite(m_image,m_imgX, m_imgY, m_posX, m_posY, m_width, m_height);
	SDL_FreeSurface(imagerotated);	 
	
}


anything with m_ in front is a private class member the code works if i leave only this line in the method
m_graphics->drawsprite(m_image,m_imgX, m_imgY, m_posX, m_posY, m_width, m_height);

any ideas on why i'm getting that segmentation fault? any help is apreciated :).

Share this post


Link to post
Share on other sites
I think this line

m_graphics->drawsprite(m_image,m_imgX, m_imgY, m_posX, m_posY, m_width, m_height);

is the problem. I haven't worked with SDL, but my intuition tells me you need to adjust your width/height after rotating a surface. Looking at the sdl_rotatezoom header I found a function that may help you:

void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth,int *dstheight);

On a side note, what is this line for?

m_image=imagerotated;

It looks like a memory leak.

[Edited by - deathkrush on May 6, 2006 3:51:22 PM]

Share this post


Link to post
Share on other sites
Oh, i see it now, when you do

SDL_FreeSurface(imagerotated);

That kills both imagerotated and m_image pointers. You need to find a way to free the surface without losing your pointers. This should do it:

imagerotated=rotozoomSurface(m_image, m_currentAngle,1,0);
SDL_FreeSurface(m_image);
m_image=imagerotated;
m_graphics->drawsprite(m_image,m_imgX, m_imgY, m_posX, m_posY, m_width, m_height);

Share this post


Link to post
Share on other sites
how does it kill both of my pointers? don't i free only the imagerotated surface with freesurface?

and i still get the segmentation fault even after adding that line (though it wouldn't be practical because i don't want to loose the image in the m_image surface because it's a class member not a local variable)

Share this post


Link to post
Share on other sites
i think this may be what you want.

void SDLCharacter::draw()
{
SDL_Surface *imagerotated=rotozoomSurface(m_image, m_currentAngle,1,0);
m_graphics->drawsprite(imagerotated,m_imgX, m_imgY, m_posX, m_posY, m_width, m_height);
SDL_FreeSurface(imagerotated);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Hypherion
how does it kill both of my pointers? don't i free only the imagerotated surface with freesurface?

and i still get the segmentation fault even after adding that line (though it wouldn't be practical because i don't want to loose the image in the m_image surface because it's a class member not a local variable)


Well, when you do

m_image=imagerotated;

that pretty loses whatever data m_image was pointing too. When you free imagerotated surface, both m_image and imagerotated pointers end up pointing into nothing.

Share this post


Link to post
Share on other sites
deathkrush: thanks for the explanation

gumpy:thanks...now it doesn't crash...but it only shows 1/4 of the image :|..any ideas why?

Share this post


Link to post
Share on other sites
are you blitting using the dimensions of the original, non-rotated surface instead of the dimensions of the rotated surface?

Share this post


Link to post
Share on other sites
you are right...that is it i'mt rying to show the image with the old coordinates.

the thing is now...how do i use the new image then to blit it? since i should get a diamond shape from a sqared one?

(sry for the newbie questions :) )

Share this post


Link to post
Share on other sites
Quote:
Original post by Hypherion
you are right...that is it i'mt rying to show the image with the old coordinates.

the thing is now...how do i use the new image then to blit it? since i should get a diamond shape from a sqared one?

(sry for the newbie questions :) )


i forgot to check back here...

use the information stored in the new surface to get what you need. see SDL_Surface. the w and h members contain the width and height of the surface.

this is a good bookmark for sdl users

Share this post


Link to post
Share on other sites

This topic is 4270 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this