Sign in to follow this  
JasonL220

Wierd error ( possibly allocation )

Recommended Posts

I have been writing some image rotation but when it get to the line where is want an array of Point object it crashes, no warning no nothing the debugger in msvc ee just offers disassembly. the function under quastion is:
// return a rotated bitmap
SDL_Surface *rotateimage(SDL_Surface *src, float theta, bool square)
{
	// new size
	int tempwidth, tempheight;

	// wrap theta between -180 and 180
	if( theta > 180 )
		theta -= 360;

	// convert angle to radians
	float angle = DegToRad(theta);

	// pre-calculate sin & cos
	float cosine = cos(angle);
	float sine = sqrt( 1 - ( cosine*cosine ) );

	// calculate size of new image
	if( square == true )
	{
		tempwidth = tempheight = (int)ceil( src->w * M_SQRT_2 * sin( ( angle ) + M_PI_4 ) );
	}
	else
	{
		tempwidth	= (int)ceil( src->h * cos( angle ) + src->w * sin( angle ) );
		tempheight	= (int)ceil( src->h * sin( angle ) + src->w * cos( angle ) );
	}

	// create new surface
	Rotated = SDL_CreateRGBSurface( SDL_HWSURFACE, tempwidth, tempheight, 
		src->format->BitsPerPixel, rmask, gmask, bmask, amask);

	// Fill image with bright pink
	Uint32 colorkeypink = SDL_MapRGB( Rotated->format, 0xFF, 0, 0xFF );
	SDL_FillRect(Rotated, NULL, colorkeypink);

	[b]// 2 array containing the endpoints
	Point * End1 = new Point[src->h];
	Point * End2 = new Point[src->h];[/b]

	// calculate new origin
	int x0	= tempwidth	 >> 1;
	int y0	= tempheight >> 1;

	// calculate new position for upper-left corner
	float ulx = (float)-x0;	// translate
	float uly = (float)-y0;	// translate

	float ulrx = ulx*cosine - uly*sine;	// rotate
	float ulry = ulx*sine + uly*cosine;	// rotate

	ulrx = ceil( ulrx + x0 );	// untranslate
	ulry = ceil( ulry + y0 );	// untranslate

	// place the upper-left pixel in the new image
	putpixel( Rotated, ulrx, ulry, getpixel( src, 0, 0 ) );

	// calculate new position for upper-right corner
	float urx = (float)(src->w - x0);	// translate
	float ury = (float)-y0;				// translate

	float urrx = urx*cosine - ury*sine;	// Rotate
	float urry = urx*sine + ury*cosine;	// Rotate

	urrx = ceil( urrx + x0 );	// untranslate
	urry = ceil( urry + y0 );	// untranslate

	// place the upper-right pixel in the new image
	putpixel( Rotated, urrx, urry, getpixel( src, src->w, 0 ) );

	// calculate new position for lower-right corner
	float lrx = (float)(src->w - x0);	// translate
	float lry = (float)(src->h - y0);	// translate

	float lrrx = lrx*cosine - lry*sine;	// Rotate
	float lrry = lrx*sine + lry*cosine;	// Rotate

	lrrx = ceil( lrrx + x0 );	// untranslate
	lrry = ceil( lrry + y0 );	// untranslate

	// place the lower-right pixel in the new image
	putpixel( Rotated, lrrx, lrry, getpixel( src, src->w, src->h ) );

	// calculate new position for lower-left corner
	float llx = (float)-x0;				// translate
	float lly = (float)(src->h - y0);	// translate

	float llrx = llx*cosine - lly*sine;	// Rotate
	float llry = lly*sine + lly*cosine;	// Rotate

	llrx = ceil( llrx + x0 );	// untranslate
	llry = ceil( llry + y0 );	// untranslate

	// place the lower-left pixel in the new image
	putpixel( Rotated, llrx, llry, getpixel( src, 0, src->h ) );

	// now we have the four corners use Bresenham's line algorithm to
	// find the position of the pixel between UL & LL and UR & LR
	BresenhamLine( ulrx, llrx, ulry, llry, 1 );
	BresenhamLine( urrx, lrrx, urry, lrry, 1 );

	// loop through the horisonal line on orginal image
	for( int y = 0; y < src->h; y++)
	{
		BresenhamLine( End1[y].x, End2[y].x, End1[y].y, End2[y].y, 0 );
	}
	return Rotated;
}





the offending line(s) have been highlighted could anyone shead some light on this? many thanks

Share this post


Link to post
Share on other sites
My guess would be that src->h is either less than or equal to zero, or it is larger than the amount of memory the system can allocate for you. Since you use the src pointer elsewhere, I suppose it's safe to assume that it is a valid pointer; otherwise, accessing ->h would likely cause a crash.

When it breaks in assembly, then you're probably within the code of a system call. However, if you enable the stack trace window, you should be able to see the function stack, and double-clicking on any of them should show you where you are within that particular function. If you find rotateimage() in the function stack, you should be able to then check out the values of various variables, such as src->h.

Share this post


Link to post
Share on other sites
i've tried that and src->h is definetly 60 i wouldn't have though i was useout of memory as i'll only allocating 60 2 int structs, so about 480bytes which isn't alot. it really wierd

Share this post


Link to post
Share on other sites
In that case, my next guess is more unpleasant, because the problem can sometimes be significantly harder to find.

It's quite possible that you've written to memory that shouldn't have been written to. Usually this is in the form of writing beyond the end of an array. In this case, you're still within "legal territory", so the system doesn't really know you're doing anything naughty, and the memory gets written just fine, no errors. However, you might have very likely written over important information that the memory manager was using to know which memory was allocated, and which wasn't. So the next time you try to do something with the memory manager (new or delete), it finds some bogus data, gets really confused, and throws an error.

The reason it can be so hard to track down is because the real error happens sometime before you get any indication that there was an error, sometimes quite a while before. Try looking in your code for accesses to arrays, and verify that they'll never access stuff beyond the array's bounds.

Share this post


Link to post
Share on other sites

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