CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2004
    Posts
    198

    new in one class and delete in another

    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.

    Code:
    	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.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: new in one class and delete in another

    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?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Oct 2004
    Posts
    198

    Re: new in one class and delete in another

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured