[OPENGL32]
[reactos.git] / reactos / dll / win32 / opengl32 / opengl32.c
index 2dd3312..e3c5602 100644 (file)
@@ -34,7 +34,7 @@ OPENGL32_ThreadAttach( void )
 
     dispatchTable = (PROC*)HeapAlloc( GetProcessHeap(),
                                       HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY,
-                                      sizeof (((ICDTable *)(0))->dispatch_table) );
+                                      DISPATCH_TABLE_SIZE );
     if (dispatchTable == NULL)
     {
         DBGPRINT( "Error: Couldn't allocate GL dispatch table" );
@@ -72,7 +72,7 @@ static void
 OPENGL32_ThreadDetach( void )
 {
     GLTHREADDATA* lpData = NULL;
-    PROC *dispatchTable = NULL;
+       TEB* teb = NtCurrentTeb();
 
     rosglMakeCurrent( NULL, NULL );
 
@@ -84,13 +84,17 @@ OPENGL32_ThreadDetach( void )
                       GetLastError() );
         lpData = NULL;
     }
+    TlsSetValue( OPENGL32_tls, NULL );
 
-    dispatchTable = NtCurrentTeb()->glTable;
-    if (dispatchTable != NULL)
+    if (teb->glTable != NULL)
     {
-        if (!HeapFree( GetProcessHeap(), 0, dispatchTable ))
+        if (!HeapFree( GetProcessHeap(), 0, teb->glTable ))
+               {
             DBGPRINT( "Warning: HeapFree() on dispatch table failed (%d)",
                       GetLastError() );
+               }
+               /* NULL-ify it. Even if something went wrong, it's not a good idea to keep it non NULL */
+               teb->glTable = NULL;
     }
 }
 
@@ -472,8 +476,6 @@ OPENGL32_LoadICD( LPCWSTR driver )
     {
         if (!_wcsicmp( driver, icd->driver_name )) /* found */
         {
-            icd->refcount++;
-
             /* release mutex */
             if (!ReleaseMutex( OPENGL32_processdata.driver_mutex ))
                 DBGPRINT( "Error: ReleaseMutex() failed (%d)", GetLastError() );
@@ -484,8 +486,6 @@ OPENGL32_LoadICD( LPCWSTR driver )
 
     /* not found - try to load */
     icd = OPENGL32_LoadDriver( driver );
-    if (icd != NULL)
-        icd->refcount = 1;
 
     /* release mutex */
     if (!ReleaseMutex( OPENGL32_processdata.driver_mutex ))
@@ -513,7 +513,7 @@ OPENGL32_UnloadICD( GLDRIVERDATA *icd )
         return FALSE; /* FIXME: do we have to expect such an error and handle it? */
     }
 
-    if (--icd->refcount == 0)
+    if (icd->refcount == 0)
         ret = OPENGL32_UnloadDriver( icd );
 
     /* release mutex */