Weird malloc error when dealing with lots of NSImage objects
Weird malloc error when dealing with lots of NSImage objects
- Subject: Weird malloc error when dealing with lots of NSImage objects
- From: DairyKnight <email@hidden>
- Date: Mon, 7 Sep 2009 19:45:33 +0800
Hi all,
I keep on getting weird malloc errors with the following code, first from
ATSFontManager, and then:
FooProgram(1343,0xa01e1720) malloc: *** mmap(size=30855168) failed (error
code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
2009-09-07 19:37:17.606 KindPDF[1343:10b] *** NSCopyMemoryPages(0xfc18a000,
0x0, 30851072) failed
It seems to me that somewhere I have a memory leak, but I just cannot figure
out where.
What the function does is to take in a PDFPage and crop according to some
cropRect and scale down to
render the page on some surface with arbitrary geometry.
The program fails after processed for around 400 images with a device size
of 786x1144 and RENDER_SCALE_FACTOR
of 2. MAX_POINTS of 300 (This constant is to make the cropRect units
percetages)
Hope someone could help me.... this program has been making me mad for
around a week... Thanks.
-(NSImage *) generatePage:(PDFPage *) page withCropRect:(CropRect) cropRect
onDevice:(NSSize) deviceSize
{
NSRect originalPageMediaBox = [page boundsForBox:kPDFDisplayBoxMediaBox];
NSRect croppedPageMediaBox = NSMakeRect((float)originalPageMediaBox.size.
width * (float)cropRect.left / MAX_POINTS,
(float)originalPageMediaBox.size.height * (float)cropRect.bottom /
MAX_POINTS,
originalPageMediaBox.size.width * (1.0 - (float)cropRect.horiCropped /
MAX_POINTS),
originalPageMediaBox.size.height * (1.0 - (float)cropRect.vertCropped /
MAX_POINTS));
float croppedPDFAspectRatio = croppedPageMediaBox.size.height /
croppedPageMediaBox.size.width;
float deviceAspectRatio = deviceSize.height / deviceSize.width;
float width, height;
if (croppedPDFAspectRatio > deviceAspectRatio)
{
height = deviceSize.height;
width = height / croppedPDFAspectRatio;
} else
{
width = deviceSize.width;
height = width * croppedPDFAspectRatio;
}
width = width * RENDER_SCALE_FACTOR;
height = height * RENDER_SCALE_FACTOR;
//
// Crop the PDF Page
//
NSPDFImageRep *pdfImg = [NSPDFImageRep imageRepWithData:[page
dataRepresentation]];
NSSize pageSize = NSMakeSize(width / (1.0 - cropRect.horiCropped /
MAX_POINTS), height / 1.0 - cropRect.vertCropped / MAX_POINTS);
NSImage *pdfPage = [[NSImage alloc] initWithSize:pageSize];
[pdfPage lockFocus];
[pdfImg drawInRect:[pdfPage rect]];
[pdfPage unlockFocus];
NSBitmapImageRep *pdfPageRep = [NSBitmapImageRep imageRepWithData:[pdfPage
TIFFRepresentation]];
NSImage *rasterizedPage = [[NSImage alloc] initWithSize:NSMakeSize(width,
height)];
[rasterizedPage lockFocus];
[[NSColor whiteColor] setFill];
NSRectFill(NSMakeRect(0, 0, width, height));
[pdfPageRep drawAtPoint:NSMakePoint(-1 * width * cropRect.left / MAX_POINTS,
-1 * height * cropRect.bottom / MAX_POINTS)];
[rasterizedPage unlockFocus];
[pdfPage release];
//
// Scale down to original size
//
NSImage *surface = [[[NSImage alloc] initWithSize:NSMakeSize(width /
RENDER_SCALE_FACTOR, height / RENDER_SCALE_FACTOR)] autorelease];
[surface lockFocus];
[rasterizedPage drawInRect:[surface rect] fromRect:[rasterizedPage rect]
operation:NSCompositeCopy fraction:1];
[surface unlockFocus];
[rasterizedPage release];
return surface;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden