Fix the USER32 DLL initialization and cleanup routines to prevent memory/resource...
authorFilip Navara <filip.navara@gmail.com>
Mon, 12 Dec 2005 20:15:23 +0000 (20:15 +0000)
committerFilip Navara <filip.navara@gmail.com>
Mon, 12 Dec 2005 20:15:23 +0000 (20:15 +0000)
svn path=/trunk/; revision=20110

reactos/lib/user32/include/menu.h
reactos/lib/user32/include/message.h
reactos/lib/user32/include/user32p.h
reactos/lib/user32/misc/dllmain.c
reactos/lib/user32/windows/menu.c
reactos/lib/user32/windows/message.c

index 503da0c..6421250 100644 (file)
@@ -13,6 +13,8 @@ MenuDrawMenuBar(HDC hDC, LPRECT Rect, HWND hWnd, BOOL Draw);
 BOOL
 MenuInit(VOID);
 VOID
+MenuCleanup(VOID);
+VOID
 MenuTrackMouseMenuBar(HWND hWnd, ULONG Ht, POINT Pt);
 VOID
 MenuTrackKbdMenuBar(HWND hWnd, ULONG wParam, ULONG Key);
index 39ad489..133c77c 100644 (file)
@@ -10,5 +10,6 @@
 #define LIB_USER32_INCLUDE_MESSAGE_H
 
 BOOL FASTCALL MessageInit(VOID);
+VOID FASTCALL MessageCleanup(VOID);
 
 #endif /* LIB_USER32_INCLUDE_MESSAGE_H */
index c981b3f..e4d9f03 100644 (file)
@@ -127,5 +127,10 @@ DEVMODEW *
 STDCALL
 GdiConvertToDevmodeW(DEVMODEA *dm);
 
+/* FIXME: Belongs to some header. */
+BOOL STDCALL GdiDllInitialize(HANDLE, DWORD, LPVOID);
+void InitStockObjects(void);
+VOID DeleteFrameBrushes(VOID);
+
 #endif
 /* EOF */
index b32f9f3..a1d8a6f 100644 (file)
 #define NDEBUG
 #include <debug.h>
 
-/* FIXME: Belongs to some header. */
-BOOL STDCALL GdiDllInitialize(HANDLE, DWORD, LPVOID);
-void InitStockObjects(void);
-VOID DeleteFrameBrushes(VOID);
-
-extern CRITICAL_SECTION gcsMPH;
 static ULONG User32TlsIndex;
 HINSTANCE User32Instance;
-HWINSTA ProcessWindowStation;
 
 PUSER32_THREAD_DATA
 User32GetThreadData()
 {
-  return((PUSER32_THREAD_DATA)TlsGetValue(User32TlsIndex));
+   return ((PUSER32_THREAD_DATA)TlsGetValue(User32TlsIndex));
 }
 
-VOID
+BOOL
 InitThread(VOID)
 {
-  PUSER32_THREAD_DATA ThreadData;
-
-  ThreadData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
-                        sizeof(USER32_THREAD_DATA));
-  TlsSetValue(User32TlsIndex, ThreadData);
+   PUSER32_THREAD_DATA ThreadData;
+
+   ThreadData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
+                          sizeof(USER32_THREAD_DATA));
+   if (ThreadData == NULL)
+      return FALSE;
+   if (!TlsSetValue(User32TlsIndex, ThreadData))
+      return FALSE;
+   return TRUE;
 }
 
 VOID
 CleanupThread(VOID)
 {
-  PUSER32_THREAD_DATA ThreadData;
+   PUSER32_THREAD_DATA ThreadData;
 
-  ThreadData = (PUSER32_THREAD_DATA)TlsGetValue(User32TlsIndex);
-  HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, ThreadData);
-  TlsSetValue(User32TlsIndex, 0);
+   ThreadData = (PUSER32_THREAD_DATA)TlsGetValue(User32TlsIndex);
+   HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, ThreadData);
+   TlsSetValue(User32TlsIndex, 0);
 }
 
-VOID
+BOOL
 Init(VOID)
 {
-  /* Set up the kernel callbacks. */
-  NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_WINDOWPROC] =
-    (PVOID)User32CallWindowProcFromKernel;
-  NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_SENDASYNCPROC] =
-    (PVOID)User32CallSendAsyncProcForKernel;
-  NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_LOADSYSMENUTEMPLATE] =
-    (PVOID)User32LoadSysMenuTemplateForKernel;
-  NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_LOADDEFAULTCURSORS] =
-    (PVOID)User32SetupDefaultCursors;
-  NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_HOOKPROC] =
-    (PVOID)User32CallHookProcFromKernel;
-
-  /* Allocate an index for user32 thread local data. */
-  User32TlsIndex = TlsAlloc();
-
-  MenuInit();
-  MessageInit();
-
-  InitializeCriticalSection(&U32AccelCacheLock);
-  InitializeCriticalSection(&gcsMPH);
-
-  GdiDllInitialize(NULL, DLL_PROCESS_ATTACH, NULL);
-  InitStockObjects();
+   /* Set up the kernel callbacks. */
+   NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_WINDOWPROC] =
+      (PVOID)User32CallWindowProcFromKernel;
+   NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_SENDASYNCPROC] =
+      (PVOID)User32CallSendAsyncProcForKernel;
+   NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_LOADSYSMENUTEMPLATE] =
+      (PVOID)User32LoadSysMenuTemplateForKernel;
+   NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_LOADDEFAULTCURSORS] =
+      (PVOID)User32SetupDefaultCursors;
+   NtCurrentTeb()->ProcessEnvironmentBlock->KernelCallbackTable[USER32_CALLBACK_HOOKPROC] =
+      (PVOID)User32CallHookProcFromKernel;
+
+   /* Allocate an index for user32 thread local data. */
+   User32TlsIndex = TlsAlloc();
+   if (User32TlsIndex != TLS_OUT_OF_INDEXES)
+   {
+      if (MessageInit())
+      {
+         if (MenuInit())
+         {
+            InitializeCriticalSection(&U32AccelCacheLock);
+            GdiDllInitialize(NULL, DLL_PROCESS_ATTACH, NULL);
+            InitStockObjects();
+
+            return TRUE;
+         }
+         MessageCleanup();
+      }
+      TlsFree(User32TlsIndex);
+   }
+
+   return FALSE;
 }
 
 VOID
 Cleanup(VOID)
 {
-  GdiDllInitialize(NULL, DLL_PROCESS_DETACH, NULL);
-
-  TlsFree(User32TlsIndex);
+   DeleteCriticalSection(&U32AccelCacheLock);
+   MenuCleanup();
+   MessageCleanup();
+   DeleteFrameBrushes();
+   GdiDllInitialize(NULL, DLL_PROCESS_DETACH, NULL);
+   TlsFree(User32TlsIndex);
 }
 
-
-
 INT STDCALL
 DllMain(
-       PVOID  hinstDll,
-       ULONG  dwReason,
-       PVOID  reserved
-       )
+   IN PVOID hInstanceDll,
+   IN ULONG dwReason,
+   IN PVOID reserved)
 {
-  switch (dwReason)
-    {
-    case DLL_PROCESS_ATTACH:
-      User32Instance = hinstDll;
-      hProcessHeap = RtlGetProcessHeap();
-      Init();
-      InitThread();
-      break;
-    case DLL_THREAD_ATTACH:
-      InitThread();
-      break;
-    case DLL_THREAD_DETACH:
-      CleanupThread();
-      break;
-    case DLL_PROCESS_DETACH:
-      DeleteFrameBrushes();
-      CleanupThread();
-      Cleanup();
-      break;
-    }
-  return(1);
+   switch (dwReason)
+   {
+      case DLL_PROCESS_ATTACH:
+         User32Instance = hInstanceDll;
+         hProcessHeap = RtlGetProcessHeap();
+         if (!Init())
+            return FALSE;
+         if (!InitThread())
+         {
+            Cleanup();
+            return FALSE;
+         }
+         break;
+
+      case DLL_THREAD_ATTACH:
+         if (!InitThread())
+            return FALSE;
+         break;
+
+      case DLL_THREAD_DETACH:
+         CleanupThread();
+         break;
+
+      case DLL_PROCESS_DETACH:
+         CleanupThread();
+         Cleanup();
+         break;
+   }
+
+   return TRUE;
 }
index e19d666..65d1749 100644 (file)
@@ -1077,6 +1077,8 @@ MenuInit(VOID)
     if(hMenuFontBold == NULL)
     {
       DbgPrint("MenuInit(): CreateFontIndirectW(hMenuFontBold) failed!\n");
+      DeleteObject(hMenuFont);
+      hMenuFont = NULL;
       return FALSE;
     }
   }
@@ -1085,6 +1087,24 @@ MenuInit(VOID)
 }
 
 
+VOID
+MenuCleanup(VOID)
+{
+  if (hMenuFont)
+  {
+    DeleteObject(hMenuFont);
+    hMenuFont = NULL;
+  }
+
+  if (hMenuFontBold)
+  {
+    DeleteObject(hMenuFontBold);
+    hMenuFontBold = NULL;
+  }
+}
+
+
+
 /***********************************************************************
  *           MenuCalcItemSize
  *
index 2877285..3169308 100644 (file)
@@ -2107,14 +2107,22 @@ MsgWaitForMultipleObjects(
 }
 
 
-BOOL FASTCALL MessageInit()
+BOOL FASTCALL MessageInit(VOID)
 {
   InitializeCriticalSection(&DdeCrst);
   InitializeCriticalSection(&MsgConversionCrst);
+  InitializeCriticalSection(&gcsMPH);
 
   return TRUE;
 }
 
+VOID FASTCALL MessageCleanup(VOID)
+{
+  DeleteCriticalSection(&DdeCrst);
+  DeleteCriticalSection(&MsgConversionCrst);
+  DeleteCriticalSection(&gcsMPH);
+}
+
 /***********************************************************************
  *             map_wparam_AtoW
  *