[1/2] gdiplus: Don't keep a reference to HDC in the GpBitmap object.

Dmitry Timoshkov dmitry at baikal.ru
Fri Jul 27 00:09:56 CDT 2012


There is no real need for that, and the test added in 2/2 crashes because
an image passed to GdipGetImageGraphicsContext() is destroyed right after
the graphics object was created, then later on all calls using that HDC fail
and silently lead to using invalid or not initilazed values.
---
 dlls/gdiplus/gdiplus_private.h |  1 -
 dlls/gdiplus/graphics.c        | 21 +++++----------------
 dlls/gdiplus/image.c           | 14 ++------------
 3 files changed, 7 insertions(+), 29 deletions(-)

diff --git a/dlls/gdiplus/gdiplus_private.h b/dlls/gdiplus/gdiplus_private.h
index 0c7e055..9dfc7fa 100644
--- a/dlls/gdiplus/gdiplus_private.h
+++ b/dlls/gdiplus/gdiplus_private.h
@@ -305,7 +305,6 @@ struct GpBitmap{
     INT numlocks;
     BYTE *bitmapbits;   /* pointer to the buffer we passed in BitmapLockBits */
     HBITMAP hbitmap;
-    HDC hdc;
     BYTE *bits; /* actual image bits if this is a DIB */
     INT stride; /* stride of bits if this is a DIB */
     BYTE *own_bits; /* image bits that need to be freed with this object */
diff --git a/dlls/gdiplus/graphics.c b/dlls/gdiplus/graphics.c
index 5ad2f56..f6eb6db 100644
--- a/dlls/gdiplus/graphics.c
+++ b/dlls/gdiplus/graphics.c
@@ -3236,7 +3236,7 @@ GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image
         else
         {
             HDC hdc;
-            int temp_hdc=0, temp_bitmap=0;
+            int temp_bitmap=0;
             HBITMAP hbitmap, old_hbm=NULL;
 
             if (!(bitmap->format == PixelFormat16bppRGB555 ||
@@ -3250,7 +3250,6 @@ GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image
 
                 /* we can't draw a bitmap of this format directly */
                 hdc = CreateCompatibleDC(0);
-                temp_hdc = 1;
                 temp_bitmap = 1;
 
                 bih.biSize = sizeof(BITMAPINFOHEADER);
@@ -3279,17 +3278,10 @@ GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image
                     bitmap->image.palette);
             }
             else
-            {
                 hbitmap = bitmap->hbitmap;
-                hdc = bitmap->hdc;
-                temp_hdc = (hdc == 0);
-            }
 
-            if (temp_hdc)
-            {
-                if (!hdc) hdc = CreateCompatibleDC(0);
-                old_hbm = SelectObject(hdc, hbitmap);
-            }
+            hdc = CreateCompatibleDC(0);
+            old_hbm = SelectObject(hdc, hbitmap);
 
             if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
             {
@@ -3302,11 +3294,8 @@ GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image
                     hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
             }
 
-            if (temp_hdc)
-            {
-                SelectObject(hdc, old_hbm);
-                DeleteDC(hdc);
-            }
+            SelectObject(hdc, old_hbm);
+            DeleteDC(hdc);
 
             if (temp_bitmap)
                 DeleteObject(hbitmap);
diff --git a/dlls/gdiplus/image.c b/dlls/gdiplus/image.c
index f415a45..4c6fee7 100644
--- a/dlls/gdiplus/image.c
+++ b/dlls/gdiplus/image.c
@@ -1906,7 +1906,6 @@ GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
     (*bitmap)->image.picture = NULL;
     (*bitmap)->image.stream = NULL;
     (*bitmap)->hbitmap = hbitmap;
-    (*bitmap)->hdc = NULL;
     (*bitmap)->bits = bits;
     (*bitmap)->stride = stride;
     (*bitmap)->own_bits = own_bits;
@@ -2099,7 +2098,6 @@ static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
 
     GdipFree(dst->bitmapbits);
     GdipFree(dst->own_bits);
-    DeleteDC(dst->hdc);
     DeleteObject(dst->hbitmap);
 
     if (clobber_palette)
@@ -2116,7 +2114,6 @@ static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
     dst->height = src->height;
     dst->format = src->format;
     dst->hbitmap = src->hbitmap;
-    dst->hdc = src->hdc;
     dst->bits = src->bits;
     dst->stride = src->stride;
     dst->own_bits = src->own_bits;
@@ -2143,7 +2140,6 @@ static GpStatus free_image_data(GpImage *image)
     {
         GdipFree(((GpBitmap*)image)->bitmapbits);
         GdipFree(((GpBitmap*)image)->own_bits);
-        DeleteDC(((GpBitmap*)image)->hdc);
         DeleteObject(((GpBitmap*)image)->hbitmap);
         if (((GpBitmap*)image)->metadata_reader)
             IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
@@ -2284,7 +2280,6 @@ GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
     GpGraphics **graphics)
 {
-    HDC hdc;
     GpStatus stat;
 
     TRACE("%p %p\n", image, graphics);
@@ -2294,13 +2289,8 @@ GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
 
     if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
     {
-        hdc = ((GpBitmap*)image)->hdc;
-
-        if(!hdc){
-            hdc = CreateCompatibleDC(0);
-            SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
-            ((GpBitmap*)image)->hdc = hdc;
-        }
+        HDC hdc = CreateCompatibleDC(0);
+        SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
 
         stat = GdipCreateFromHDC(hdc, graphics);
 
-- 
1.7.11.2




More information about the wine-patches mailing list