Archived

This topic is now archived and is closed to further replies.

shadowman13131

Funky Memory Allocation Glitch

Recommended Posts

Hey, I''ve tried to solve this problem for awhile, but haven''t thought of any possible problems... Here''s the deal: I''m making a function that generates a strip of rotated bitmaps for some stuff i''m working on in this one game. And so i have a function which takes the number of desired frames and repeatedly rotates a bitmap and attaches the product to the end of a bitmap until the bitmap''s been rotated 360 degrees in that many frames. The procedure works perfectly-unless you enter a power of 2 for the number of frames. Then the time after it''s rotated exactly 270 degrees (i know it''s really odd conditions but i''ve tested a lot and that seems to be it) the memory allocation for the new rotated bitmap is off... Here''s a code snippet from the function that calls the rotate bitmap: /////////////////////////////////////////////////////////// for (int XZ = 0; XZ < frames; XZ++) { LBitmap* abc; abc = RotateBitmap(NULL,bmp1,degrees * (float)XZ); int yoff = (int)((ret->height - abc->height)/2); int xoff = (int)((bmp1->width - abc->width)/2); ret->Decal(abc,bmp1->width * XZ + xoff,yoff,1,false); //delete [] abc->bitmap; delete abc; } //////////////////////////////////////////////////////////// By the way, LBitmap is a class I made to load up bitmaps and manipulate them, and rotate bitmap is this function: ///////////////////////////////////////////////////////////// POINT RotatePoint(POINT p, float deg) { POINT P; deg = (float)DEGTORAD(deg); float X = (p.x*cos(deg) + p.y*-sin(deg)); float Y = (p.x*sin(deg) + p.y*cos(deg)); if (X - (int)X <= 0.99999999f) X = (float)floor(X); else X = (float)ceil(X); if (Y - (int)Y <= 0.99999999f) Y = (float)floor(Y); else Y = (float)ceil(Y); P.x = (long)X; P.y = (long)Y; return P; } LBitmap* RotateBitmap(char* savefile, LBitmap* bitmap, float degrees) { LBitmap* ret = new LBitmap; int width = bitmap->width; int height = bitmap->height; POINT a[4]; a[0].x = 0; a[0].y = 0; a[1].x = width; a[1].y = 0; a[2].x = width; a[2].y = height; a[3].x = 0; a[3].y = height; a[0] = RotatePoint(a[0],degrees); a[1] = RotatePoint(a[1],degrees); a[2] = RotatePoint(a[2],degrees); a[3] = RotatePoint(a[3],degrees); int xmin = 10000; int xmax = -10000; int ymin = 10000; int ymax = -10000; for (int k = 0; k < 4; k++) { if (a[k].x > xmax) xmax = a[k].x; if (a[k].x < xmin) xmin = a[k].x; if (a[k].y > ymax) ymax = a[k].y; if (a[k].y < ymin) ymin = a[k].y; } width = xmax - xmin; height = ymax - ymin; //if (degrees > 270) width++; BITMAPFILEHEADER* bmfh; bmfh = (BITMAPFILEHEADER*)ret->headerinfo; BITMAPINFOHEADER* bmih; bmih = (BITMAPINFOHEADER*)&ret->headerinfo[sizeof(BITMAPFILEHEADER)]; //file header settings bmfh->bfType = 19778; bmfh->bfSize = 54 + width * height * 3; bmfh->bfReserved1 = 0; bmfh->bfReserved2 = 0; bmfh->bfOffBits = 54; bmih->biSize = 40; bmih->biWidth = width; bmih->biHeight = height; bmih->biPlanes = 1; bmih->biBitCount = 24; bmih->biCompression = 0; bmih->biSizeImage = 54 + width * height * 3; bmih->biXPelsPerMeter = 0; bmih->biYPelsPerMeter = 0; bmih->biClrUsed = 0; bmih->biClrImportant = 0; ret->width = width; ret->height = height; ret->scanlinesize = width * 3; ret->bitmap = new RGBColor[width * height]; for (int Y = 0; Y < height; Y++) { for (int X = 0; X < width; X++) { ret->bitmap[Y * width + X].R = 255; ret->bitmap[Y * width + X].G = 0; ret->bitmap[Y * width + X].B = 255; } } for (Y = 0; Y < bitmap->height; Y++) { for (int X = 0; X < bitmap->width; X++) { int midX = bitmap->width/2; int midY = bitmap->height/2; POINT p; p.x = X - midX; p.y = Y - midY; p = RotatePoint(p,degrees); //int Xz = p.x + midX; //int Yz = p.y + midY; int Xz = X; int Yz = Y; //int bmpX = ret->width/2 + X - midX; //int bmpY = ret->height/2 + Y - midY; int bmpX = p.x + midX; int bmpY = p.y + midY; bmpX = p.x + ret->width/2; bmpY = p.y + ret->height/2; if (Yz * bitmap->width + Xz < bitmap->width * bitmap->height) { if (Yz * bitmap->width + Xz >= 0) { ret->bitmap[bmpY * ret->width + bmpX].R = bitmap->bitmap[Yz * bitmap->width + Xz].R; ret->bitmap[bmpY * ret->width + bmpX].G = bitmap->bitmap[Yz * bitmap->width + Xz].G; ret->bitmap[bmpY * ret->width + bmpX].B = bitmap->bitmap[Yz * bitmap->width + Xz].B; } } } } ret->SaveAs(savefile); return ret; } //////////////////////////////////////////////////////////////// The problem in the allocation is in the line ret->bitmap = new RGBColor[width * height]; At least, that''s when the program crashes... But i can''t seem to find anything wrong with that line... Tell me if any of you find anything. If you need some more information about it I can probably give some more. Shadowman

Share this post


Link to post
Share on other sites
Nobody likes to read other ppl code and help them debug. It''s already enuff at work.

If it crashes at the new line (heck, it took a while to find out where it is), the problem is probably somewhere else. Check your memory allocation/deallocation procedures.

Share this post


Link to post
Share on other sites