views:

513

answers:

2

I have a custom view that draws an CGImage using:

- (void) drawImage
{
    CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
    CGRect imageRect = {{0,0}, {CGImageGetWidth(image), CGImageGetHeight(image)}};
    CGContextDrawImage(context, imageRect, image);
}

It looks like the memory used by the process increases steadily when resizing the view (and thus repeatedly calling drawImage). leaks shows no leak. vmmap shows indeed a memory increase, but in an area which is outside my application's direct control, i.e. CG raster data.

REGION TYPE           [ VIRTUAL after open]     [VIRTUAL after X resizes]
===========           [ =======]
ATS (font support)    [   31.7M]                [   31.7M]
CG backing stores     [   2448K]                [   5400K]
CG image              [     12K]                [     12K]
CG raster data        [    872K]                [   18.3M] <-- memory increase

Replacing

CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];

with

CGContextRef context = (CGContextRef)[[NSGraphicsContext graphicsContextWithWindow:[self window]] graphicsPort];

makes the leak go away but results in slower window redraws and visual artifacts.

How can I fix this?

+1  A: 

Not to be dismissive but do you really have a problem here? Leaks is all clear, 18.3M isn't very much VM for CG raster data. If you resize the window for long enough will it really consume all available memory? How have you shown this?

It peaks at about 50Mb and then it goes down. With a multi-document application 50Mb per document adds up. When using the window's graphics context the raster data never exceeds 128K.
diciu
I can't recreate it on 10.6.2. You should add some info about osx version to your post - might help someone.
+1  A: 

It doesn't seem like a leak, you are probably just increasing the size of a cache. You say it peaks at 50Mb and seem to think it will be 50Mb per document but it may simply be 50Mb per process.

benzado