Call FreeLibrary() in CGdiplusExt.

BUG=pdfium:939

Change-Id: I4204965bd8b81bea3c485fcb27adfa212cce4e69
Reviewed-on: https://pdfium-review.googlesource.com/19190
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
diff --git a/core/fxge/win32/fx_win32_gdipext.cpp b/core/fxge/win32/fx_win32_gdipext.cpp
index 1a7e132..a8066fe 100644
--- a/core/fxge/win32/fx_win32_gdipext.cpp
+++ b/core/fxge/win32/fx_win32_gdipext.cpp
@@ -112,6 +112,7 @@
   FuncId_GdipDrawString,
   FuncId_GdipSetPenTransform,
 };
+
 static LPCSTR g_GdipFuncNames[] = {
     "GdipCreatePath2",
     "GdipSetPenDashStyle",
@@ -192,6 +193,10 @@
     "GdipDrawString",
     "GdipSetPenTransform",
 };
+static_assert(FX_ArraySize(g_GdipFuncNames) ==
+                  static_cast<size_t>(FuncId_GdipSetPenTransform) + 1,
+              "g_GdipFuncNames has wrong size");
+
 typedef GpStatus(WINGDIPAPI* FuncType_GdipCreatePath2)(GDIPCONST GpPointF*,
                                                        GDIPCONST BYTE*,
                                                        INT,
@@ -672,33 +677,32 @@
   CallFunc(GdipDrawImagePointsI)(pGraphics, bitmap, destinationPoints, 3);
   CallFunc(GdipDisposeImage)(bitmap);
 }
-CGdiplusExt::CGdiplusExt() {
-  m_hModule = nullptr;
-  m_GdiModule = nullptr;
-  for (size_t i = 0; i < sizeof g_GdipFuncNames / sizeof(LPCSTR); i++) {
-    m_Functions[i] = nullptr;
-  }
-  m_pGdiAddFontMemResourceEx = nullptr;
-  m_pGdiRemoveFontMemResourseEx = nullptr;
+
+CGdiplusExt::CGdiplusExt() {}
+
+CGdiplusExt::~CGdiplusExt() {
+  FreeLibrary(m_GdiModule);
+  FreeLibrary(m_hModule);
 }
+
 void CGdiplusExt::Load() {
-  ByteString strPlusPath;
   char buf[MAX_PATH];
   GetSystemDirectoryA(buf, MAX_PATH);
-  strPlusPath += buf;
-  strPlusPath += "\\";
-  strPlusPath += "GDIPLUS.DLL";
-  m_hModule = LoadLibraryA(strPlusPath.c_str());
+  ByteString dllpath = buf;
+  dllpath += "\\GDIPLUS.DLL";
+  m_hModule = LoadLibraryA(dllpath.c_str());
   if (!m_hModule)
     return;
 
-  for (size_t i = 0; i < sizeof g_GdipFuncNames / sizeof(LPCSTR); i++) {
+  m_Functions.resize(FX_ArraySize(g_GdipFuncNames));
+  for (size_t i = 0; i < FX_ArraySize(g_GdipFuncNames); ++i) {
     m_Functions[i] = GetProcAddress(m_hModule, g_GdipFuncNames[i]);
     if (!m_Functions[i]) {
       m_hModule = nullptr;
       return;
     }
   }
+
   uintptr_t gdiplusToken;
   GdiplusStartupInput gdiplusStartupInput;
   ((FuncType_GdiplusStartup)m_Functions[FuncId_GdiplusStartup])(
@@ -714,7 +718,7 @@
       reinterpret_cast<FuncType_GdiRemoveFontMemResourceEx>(
           GetProcAddress(m_GdiModule, "RemoveFontMemResourceEx"));
 }
-CGdiplusExt::~CGdiplusExt() {}
+
 LPVOID CGdiplusExt::LoadMemFont(LPBYTE pData, uint32_t size) {
   GpFontCollection* pCollection = nullptr;
   CGdiplusExt& GdiplusExt =
diff --git a/core/fxge/win32/win32_int.h b/core/fxge/win32/win32_int.h
index c4559f4..d4f2597 100644
--- a/core/fxge/win32/win32_int.h
+++ b/core/fxge/win32/win32_int.h
@@ -10,6 +10,7 @@
 #include <windows.h>
 
 #include <memory>
+#include <vector>
 
 #include "core/fxcrt/retain_ptr.h"
 #include "core/fxge/cfx_pathdata.h"
@@ -32,6 +33,7 @@
  public:
   CGdiplusExt();
   ~CGdiplusExt();
+
   void Load();
   bool IsAvailable() { return !!m_hModule; }
   bool StretchBitMask(HDC hDC,
@@ -108,13 +110,13 @@
   bool GdiRemoveFontMemResourceEx(void* handle);
   RetainPtr<CFX_DIBitmap> LoadDIBitmap(WINDIB_Open_Args_ args);
 
-  FARPROC m_Functions[100];
-  FuncType_GdiAddFontMemResourceEx m_pGdiAddFontMemResourceEx;
-  FuncType_GdiRemoveFontMemResourceEx m_pGdiRemoveFontMemResourseEx;
+  std::vector<FARPROC> m_Functions;
+  FuncType_GdiAddFontMemResourceEx m_pGdiAddFontMemResourceEx = nullptr;
+  FuncType_GdiRemoveFontMemResourceEx m_pGdiRemoveFontMemResourseEx = nullptr;
 
  protected:
-  HMODULE m_hModule;
-  HMODULE m_GdiModule;
+  HMODULE m_hModule = nullptr;
+  HMODULE m_GdiModule = nullptr;
 };
 
 class CWin32Platform {