Skip to content

Commit 3622a59

Browse files
committed
[[ Bug 13458 ]] Fix memory leak in MCGRasterToSkBitmap()
1 parent 5022bff commit 3622a59

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

docs/notes/bugfix-13458.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Memory leak when setting the text of an image

libgraphics/src/utils.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,9 @@ bool MCGRasterToSkBitmap(const MCGRaster& p_raster, MCGPixelOwnershipType p_owne
488488
// for non-premultiplied bitmaps, allocate the space then set pixels one by one
489489
// for premultiplied bitmaps, just set the pixels in the target directly
490490
// if the copy pixels flag is set, allocate space and copy the pixels from the raster first, then set in target
491-
if (p_raster . format == kMCGRasterFormat_U_ARGB)
491+
492+
// IM-2014-09-16: [[ Bug 13458 ]] Don't copy if we're taking ownership. instead we'll premultiply in place below
493+
if (p_raster . format == kMCGRasterFormat_U_ARGB && p_ownership != kMCGPixelOwnershipTypeTake)
492494
p_ownership = kMCGPixelOwnershipTypeCopy;
493495

494496
switch (p_ownership)
@@ -545,22 +547,45 @@ bool MCGRasterToSkBitmap(const MCGRaster& p_raster, MCGPixelOwnershipType p_owne
545547
{
546548
if (p_raster . format == kMCGRasterFormat_U_ARGB)
547549
{
550+
r_bitmap.lockPixels();
551+
548552
// for non-premultiplied bitmaps, loop through the source bitmap pixel by pixel
549553
// premultiplying each pixel before writing to destination bitmap
550-
uint8_t *t_row_ptr;
551-
t_row_ptr = (uint8_t*) p_raster . pixels;
554+
uint8_t *t_dst_ptr;
555+
t_dst_ptr = (uint8_t*)r_bitmap.getPixels();
556+
uint32_t t_dst_stride;
557+
t_dst_stride = r_bitmap.rowBytes();
558+
559+
uint8_t *t_src_ptr;
560+
uint32_t t_src_stride;
561+
// IM-2014-09-16: [[ Bug 13458 ]] If we're taking ownership then the bitmap pixels are
562+
// both source and destination
563+
if (p_ownership == kMCGPixelOwnershipTypeTake)
564+
{
565+
t_src_ptr = t_dst_ptr;
566+
t_src_stride = t_dst_stride;
567+
}
568+
else
569+
{
570+
t_src_ptr = (uint8_t*)p_raster.pixels;
571+
t_src_stride = p_raster.stride;
572+
}
573+
552574
for (uint32_t y = 0; y < p_raster . height; y++)
553575
{
554-
uint32_t *t_pixel;
555-
t_pixel = (uint32_t*) t_row_ptr;
556-
uint32_t *t_dst_pxl;
557-
t_dst_pxl = r_bitmap . getAddr32(0, y);
576+
uint32_t *t_src_pixel;
577+
t_src_pixel = (uint32_t*)t_src_ptr;
578+
uint32_t *t_dst_pixel;
579+
t_dst_pixel = (uint32_t*)t_dst_ptr;
558580

559581
// IM-2014-07-23: [[ Bug 12892 ]] Use MCGPixel function to premultiply native format pixels
560582
for (uint32_t x = 0; x < p_raster . width; x++)
561-
*t_dst_pxl++ = MCGPixelPreMultiplyNative(*t_pixel++);
562-
t_row_ptr += p_raster . stride;
583+
*t_dst_pixel++ = MCGPixelPreMultiplyNative(*t_src_pixel++);
584+
t_dst_ptr += t_dst_stride;
585+
t_src_ptr += t_src_stride;
563586
}
587+
588+
r_bitmap.unlockPixels();
564589
}
565590
else if (p_ownership == kMCGPixelOwnershipTypeCopy)
566591
MCMemoryCopy(r_bitmap . getPixels(), p_raster . pixels, p_raster . height * p_raster . stride);

0 commit comments

Comments
 (0)