0

I'm trying to create a visual representation of some data I have.

The function I have works and creates the image perfectly (and VERY quickly) but under instruments the Real Memory usage rockets and eventually crashes the app.

I have replaced my function with a return [UIImage imageNamed:@"blah"]; and the memory problems vanish completely.

Was wondering if someone could see why and where the memory is being taken up and how I could possibly free it again?

I have run in instruments under the allocations and leaks tool but nothing shows up in there.

The function is...

- (UIImage*)imageOfMapWithDeadColor:(UIColor *)deadColor aliveColor:(UIColor *)aliveColor
{
//translate colours into rgb components
if ([deadColor isEqual:[UIColor whiteColor]]) {
    dr = dg = db = 255;
} else if ([deadColor isEqual:[UIColor blackColor]]) {
    dr = dg = db = 0;
} else {
    [deadColor getRed:&drf green:&dgf blue:&dbf alpha:&blah];

    dr = drf * 255;
    dg = dgf * 255;
    db = dbf * 255;
}

if ([aliveColor isEqual:[UIColor whiteColor]]) {
    ar = ag = ab = 255;
} else if ([aliveColor isEqual:[UIColor blackColor]]) {
    ar = ag = ab = 0;
} else {
    [aliveColor getRed:&arf green:&agf blue:&abf alpha:&blah];

    ar = arf * 255;
    ag = agf * 255;
    ab = abf * 255;
}

//create bytes of image from the cell map
int yRef, cellRef;

unsigned char *cell_ptr = cells;

for (int y=0; y<self.height; y++)
{
    yRef = y * (self.width * 4);

    int x = 0;
    do
    {
        cellRef = yRef + 4 * x;

        if (*cell_ptr & 0x01) {
            //alive colour
            buffer[cellRef] = ar;
            buffer[cellRef + 1] = ag;
            buffer[cellRef + 2] = ab;
            buffer[cellRef + 3] = 255;
        } else {
            //dead colour
            buffer[cellRef] = dr;
            buffer[cellRef + 1] = dg;
            buffer[cellRef + 2] = db;
            buffer[cellRef + 3] = 255;
        }
        cell_ptr++;
    } while (++x < self.width);
}

//create image
imageRef = CGImageCreate(self.width, self.height, 8, 32, 4 * self.width, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);

UIImage *image = [UIImage imageWithCGImage:imageRef];

//return image
return image;
}

a few notes...

buffer is an ivar GLubyte malloc-ed in the init. cells is the original unsigned char array that I'm taking the data from.

Like I said, the function works perfectly (creating the image etc...) I just get a massive usage of memory.

Oh... this function is called LOTS, like 30 times a second or more (which I know will make a difference).

Thanks for any help.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306

1 Answers1

4

You use CGImageCreate and the docs say :

Return Value A new Quartz bitmap image. You are responsible for releasing this object by calling CGImageRelease.

Tom
  • 1,319
  • 9
  • 8
  • Amazing! Thanks! I don't know how I missed that. Works perfectly now. (Will accept as correct answer when the timer runs out) – Fogmeister Sep 18 '12 at 21:31