From 5d36c47e1f8c02964b031f366e4bdce50c98170d Mon Sep 17 00:00:00 2001 From: Eric Pouech Date: Sun, 16 Mar 2008 21:48:13 +0100 Subject: [PATCH] richedit: Fixed object leaks generated by context creation & destruction. --- dlls/riched20/caret.c | 5 ++--- dlls/riched20/context.c | 3 ++- dlls/riched20/editor.h | 2 +- dlls/riched20/paint.c | 2 +- dlls/riched20/para.c | 7 ++----- dlls/riched20/run.c | 4 ++-- dlls/riched20/wrap.c | 15 +++++---------- 7 files changed, 15 insertions(+), 23 deletions(-) diff --git a/dlls/riched20/caret.c b/dlls/riched20/caret.c index 18204e22f4..a6a62178b5 100644 --- a/dlls/riched20/caret.c +++ b/dlls/riched20/caret.c @@ -215,9 +215,8 @@ ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor, *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent; *x = run->member.run.pt.x + sz.cx; *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor); - - ME_DestroyContext(&c); - ReleaseDC(editor->hWnd, hDC); + + ME_DestroyContext(&c, editor->hWnd); return; } } diff --git a/dlls/riched20/context.c b/dlls/riched20/context.c index 56e68f78d6..161a64a4eb 100644 --- a/dlls/riched20/context.c +++ b/dlls/riched20/context.c @@ -33,7 +33,8 @@ void ME_InitContext(ME_Context *c, ME_TextEditor *editor, HDC hDC) c->dpi.cy = GetDeviceCaps(hDC, LOGPIXELSY); } -void ME_DestroyContext(ME_Context *c) +void ME_DestroyContext(ME_Context *c, HWND hWnd) { + if (hWnd) ReleaseDC(hWnd, c->hDC); DeleteObject(c->hbrMargin); } diff --git a/dlls/riched20/editor.h b/dlls/riched20/editor.h index 7b9be5e3dd..0c0d324ab4 100644 --- a/dlls/riched20/editor.h +++ b/dlls/riched20/editor.h @@ -190,7 +190,7 @@ void ME_InsertEndRowFromCursor(ME_TextEditor *editor, int nCursor); BOOL ME_ArrowKey(ME_TextEditor *ed, int nVKey, BOOL extend, BOOL ctrl); void ME_InitContext(ME_Context *c, ME_TextEditor *editor, HDC hDC); -void ME_DestroyContext(ME_Context *c); +void ME_DestroyContext(ME_Context *c, HWND release); ME_Style *GetInsertStyle(ME_TextEditor *editor, int nCursor); void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para); void ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor, diff --git a/dlls/riched20/paint.c b/dlls/riched20/paint.c index 91d5ec01f5..d5cb3c7d50 100644 --- a/dlls/riched20/paint.c +++ b/dlls/riched20/paint.c @@ -91,7 +91,7 @@ void ME_PaintContent(ME_TextEditor *editor, HDC hDC, BOOL bOnlyNew, const RECT * if (editor->nTotalLength != editor->nLastTotalLength) ME_SendRequestResize(editor, FALSE); editor->nLastTotalLength = editor->nTotalLength; - ME_DestroyContext(&c); + ME_DestroyContext(&c, NULL); } void ME_Repaint(ME_TextEditor *editor) diff --git a/dlls/riched20/para.c b/dlls/riched20/para.c index 4fb24e9259..b2f8e6c117 100644 --- a/dlls/riched20/para.c +++ b/dlls/riched20/para.c @@ -28,7 +28,6 @@ static const WCHAR wszParagraphSign[] = {0xB6, 0}; void ME_MakeFirstParagraph(ME_TextEditor *editor) { ME_Context c; - HDC hDC; PARAFORMAT2 fmt; CHARFORMAT2W cf; LOGFONTW lf; @@ -38,9 +37,8 @@ void ME_MakeFirstParagraph(ME_TextEditor *editor) ME_DisplayItem *run; ME_Style *style; - hDC = GetDC(editor->hWnd); + ME_InitContext(&c, editor, GetDC(editor->hWnd)); - ME_InitContext(&c, editor, hDC); hf = (HFONT)GetStockObject(SYSTEM_FONT); assert(hf); GetObjectW(hf, sizeof(LOGFONTW), &lf); @@ -85,8 +83,7 @@ void ME_MakeFirstParagraph(ME_TextEditor *editor) text->pLast->member.para.nCharOfs = 1; - ME_DestroyContext(&c); - ReleaseDC(editor->hWnd, hDC); + ME_DestroyContext(&c, editor->hWnd); } void ME_MarkAllForWrapping(ME_TextEditor *editor) diff --git a/dlls/riched20/run.c b/dlls/riched20/run.c index a762695805..e193bd22bc 100644 --- a/dlls/riched20/run.c +++ b/dlls/riched20/run.c @@ -551,7 +551,7 @@ int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run) { SIZE sz; ME_GetOLEObjectSize(&c, run, &sz); - ReleaseDC(editor->hWnd, c.hDC); + ME_DestroyContext(&c, editor->hWnd); if (cx < sz.cx/2) return 0; return 1; @@ -580,7 +580,7 @@ int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run) ME_DestroyString(strRunText); ME_UnselectStyleFont(&c, run->style, hOldFont); - ReleaseDC(editor->hWnd, c.hDC); + ME_DestroyContext(&c, editor->hWnd); return fit; } diff --git a/dlls/riched20/wrap.c b/dlls/riched20/wrap.c index 49c53660e8..cd052a7a24 100644 --- a/dlls/riched20/wrap.c +++ b/dlls/riched20/wrap.c @@ -467,14 +467,12 @@ static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) { } BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor) { - HWND hWnd = editor->hWnd; - HDC hDC = GetDC(hWnd); ME_DisplayItem *item; ME_Context c; BOOL bModified = FALSE; int yStart = -1, yEnd = -1; - ME_InitContext(&c, editor, hDC); + ME_InitContext(&c, editor, GetDC(editor->hWnd)); c.pt.x = 0; c.pt.y = 0; editor->nHeight = 0; @@ -510,9 +508,8 @@ BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor) { editor->nTotalLength = c.pt.y; - ME_DestroyContext(&c); - ReleaseDC(hWnd, hDC); - + ME_DestroyContext(&c, editor->hWnd); + if (bModified || editor->nTotalLength < editor->nLastTotalLength) ME_InvalidateMarkedParagraphs(editor); return bModified; @@ -520,9 +517,8 @@ BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor) { void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor) { ME_Context c; - HDC hDC = GetDC(editor->hWnd); - ME_InitContext(&c, editor, hDC); + ME_InitContext(&c, editor, GetDC(editor->hWnd)); if (editor->bRedraw) { RECT rc = c.rcView; @@ -544,8 +540,7 @@ void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor) { InvalidateRect(editor->hWnd, &rc, TRUE); } } - ME_DestroyContext(&c); - ReleaseDC(editor->hWnd, hDC); + ME_DestroyContext(&c, editor->hWnd); }