Click to See Complete Forum and Search --> : new in one class and delete in another


peterworth
February 11th, 2005, 04:03 AM
i wanted to avoid posting my code but i'm completely stuck.. can someone tell me if i'm making big mistakes in the code below? i know there is potentially a memory leak with the tilePixels pointer, but delete[] is done after the pointer has been set as a member variable in another class (in the destructor of the class). This seems like bad design to me (new in one class and delete in another), but i cant think of another way to do what i want to do.


unsigned long* pixels = tif.GetPixels();
extents = tif.GetExtents();
//split into tiles
int width = tif.GetWidth();
int height = tif.GetHeight();
int top = 0;
int bottom = min(tileSize-1, height);
int left = 0;
int right = min(tileSize-1, width);
double yRatio = (extents.GetBottom()-extents.GetTop())/height;
double xRatio = (extents.GetRight()-extents.GetLeft())/width;

do
{
unsigned int* tilePixels;
do
{
tilePixels = new unsigned int[tileSize*tileSize];
int row = 0;
for (int i = top; i <= bottom; i++)
{
memcpy(&tilePixels[row*(right-left+1)],
&pixels[(i*width)+left],(right-left+1)*4);
row++;
}
AttachTile(tilePixels,bottom-top+1,right-left+1,
Rect(extents.GetTop()+(yRatio*top),
extents.GetTop()+(yRatio*bottom),
extents.GetLeft()+(xRatio*left),
extents.GetLeft()+(xRatio*right)));
left = right+1;
right = min(right+tileSize, width);
} while (right < width);
top = bottom+1;
bottom = min(bottom+tileSize, height);
} while (bottom < height);
}

the problem is that there is an access violation in the memcpy. is this because the array indexes are out of bounds (i'm pretty sure they're not) or because i've done something silly with a pointer which is just pointing to junk or something?

thanks.

cilu
February 11th, 2005, 04:07 AM
This seems like bad design to me (new in one class and delete in another), but i cant think of another way to do what i want to do.
Why don't you exaplain your actual design and see how we can change it?

peterworth
February 11th, 2005, 04:11 AM
ok - i've got an image class with an import method (posted above) which needs to break an image up into tiles. each of these tiles is stored as an instance of a tile class. so the image object has to create the tile pixels and then give them to the tile object.