segmentation fault

Started by
8 comments, last by gumpy 17 years, 11 months ago
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 :).
"Try not to become a man of success but rather to become a man of value." - Albert Einstein
Advertisement
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]
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
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);
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
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)
"Try not to become a man of success but rather to become a man of value." - Albert Einstein
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);	}
This space for rent.
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.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
deathkrush: thanks for the explanation

gumpy:thanks...now it doesn't crash...but it only shows 1/4 of the image :|..any ideas why?
"Try not to become a man of success but rather to become a man of value." - Albert Einstein
are you blitting using the dimensions of the original, non-rotated surface instead of the dimensions of the rotated surface?
This space for rent.
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 :) )
"Try not to become a man of success but rather to become a man of value." - Albert Einstein
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
This space for rent.

This topic is closed to new replies.

Advertisement