Index: trunk/src/tools/qlibrary_p.h
===================================================================
--- trunk/src/tools/qlibrary_p.h	(revision 155)
+++ trunk/src/tools/qlibrary_p.h	(revision 156)
@@ -63,4 +63,7 @@
 public:
     QLibraryPrivate( QLibrary *lib );
+#if defined(Q_OS_OS2)
+    ~QLibraryPrivate() { derefLib( FALSE ); } 
+#endif
 
 #if defined(Q_WS_WIN)
@@ -77,4 +80,8 @@
 
 private:
+#if defined(Q_OS_OS2)
+    bool derefLib( bool doFree );
+#endif
+
     QLibrary *library;
 };
Index: trunk/src/tools/qlibrary_pm.cpp
===================================================================
--- trunk/src/tools/qlibrary_pm.cpp	(revision 155)
+++ trunk/src/tools/qlibrary_pm.cpp	(revision 156)
@@ -45,4 +45,5 @@
 #ifndef QT_H
 #include "qfile.h"
+#include "qdir.h"
 #endif // QT_H
 
@@ -55,5 +56,7 @@
 };
 
-static QMap<QString, LibInstance*> *map = 0;
+typedef QMap<QCString, LibInstance*> LibInstanceMap;
+
+static LibInstanceMap map;
 /*
   The platform dependent implementations of
@@ -70,41 +73,37 @@
 {
     if ( pHnd )
-	return TRUE;
+        return TRUE;
 
 #ifdef QT_THREAD_SUPPORT
     // protect map creation/access
     QMutexLocker locker( qt_global_mutexpool ?
-			 qt_global_mutexpool->get( &map ) : 0 );
+                         qt_global_mutexpool->get( &map ) : 0 );
 #endif // QT_THREAD_SUPPORT
 
-    if ( !map )
-	map = new QMap<QString, LibInstance*>;
-
-    QString filename = library->library();
-    if ( map->find(filename) != map->end() ) {
-	LibInstance *lib = (*map)[filename];
-	lib->refCount++;
-	pHnd = lib->module;
-    }
-    else {
-        APIRET rc = 0;
-        char errModule [256];
-        if ( (rc = DosLoadModule(
-            errModule, sizeof(errModule),
-            QFile::encodeName( filename ).data(), &pHnd
-        ))) {
+    QCString filename =
+        QFile::encodeName( QDir::convertSeparators ( library->library() ) );
+    if ( map.find(filename) != map.end() ) {
+        LibInstance *lib = map[filename];
+        lib->refCount++;
+        pHnd = lib->module;
+    } else {
+        APIRET rc = NO_ERROR;
+        char errModule [CCHMAXPATH];
+        if ( (rc = DosLoadModule( errModule, sizeof(errModule),
+                                  filename.data(), &pHnd
+        )) != NO_ERROR ) {
             pHnd = 0;
 #if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
-	    qSystemWarning( QString("Failed to load library %1 (reason: %2)!")
-                .arg( filename ).arg( errModule ), rc
+            qSystemWarning( QString("Failed to load library %1 (reason: %2)!")
+                .arg( filename.data() ).arg( errModule ), rc
             );
 #endif
         }
-	if ( pHnd ) {
-	    LibInstance *lib = new LibInstance;
-	    lib->module = pHnd;
-	    lib->refCount++;
-	    map->insert( filename, lib );
-	}
+        if ( pHnd ) {
+            LibInstance *lib = new LibInstance;
+            lib->module = pHnd;
+            lib->refCount++;
+            map.insert( filename, lib );
+        }
     }
     return pHnd != 0;
@@ -113,41 +112,5 @@
 bool QLibraryPrivate::freeLibrary()
 {
-    if ( !pHnd )
-	return TRUE;
-
-#ifdef QT_THREAD_SUPPORT
-    // protect map access
-    QMutexLocker locker( qt_global_mutexpool ?
-			 qt_global_mutexpool->get( &map ) : 0 );
-#endif // QT_THREAD_SUPPORT
-
-    bool ok = FALSE;
-    QMap<QString, LibInstance*>::iterator it;
-    for ( it = map->begin(); it != map->end(); ++it ) {
-	LibInstance *lib = *it;
-	if ( lib->module == pHnd ) {
-	    lib->refCount--;
-	    if ( lib->refCount == 0 ) {
-		ok = DosFreeModule( pHnd ) == 0;
-		if ( ok ) {
-		    map->remove( it );
-		    if ( map->count() == 0 ) {
-			delete map;
-			map = 0;
-		    }
-		}
-		delete lib;
-	    } else
-		ok = TRUE;
-	    break;
-	}
-    }
-    if ( ok )
-	pHnd = 0;
-#if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
-    else
-	qSystemWarning( "Failed to unload library!" );
-#endif
-    return ok;
+    return derefLib( TRUE );
 }
 
@@ -155,11 +118,13 @@
 {
     if ( !pHnd )
-	return 0;
+        return 0;
 
-    void* address;
-    if ( DosQueryProcAddr( pHnd, 0, f, (PFN*) &address) ) address = 0;
+    void *address = 0;
+    APIRET rc = NO_ERROR;
+    if ( (rc = DosQueryProcAddr( pHnd, 0, f, (PFN*) &address)) != NO_ERROR )
+        address = 0;
 #if defined(QT_DEBUG_COMPONENT)
     if ( !address )
-	qSystemWarning( QString("Couldn't resolve symbol \"%1\"").arg( f ) );
+        qSystemWarning( QString("Couldn't resolve symbol \"%1\"").arg( f ), rc );
 #endif
 
@@ -167,3 +132,44 @@
 }
 
+bool QLibraryPrivate::derefLib( bool doFree )
+{
+    if ( !pHnd )
+        return TRUE;
+
+#ifdef QT_THREAD_SUPPORT
+    // protect map access
+    QMutexLocker locker( qt_global_mutexpool ?
+                         qt_global_mutexpool->get( &map ) : 0 );
+#endif // QT_THREAD_SUPPORT
+
+    APIRET rc = ERROR_INVALID_HANDLE;
+    LibInstanceMap::iterator it;
+    for ( it = map.begin(); it != map.end(); ++it ) {
+        LibInstance *lib = *it;
+        if ( lib->module == pHnd ) {
+            lib->refCount--;
+            if ( lib->refCount == 0 ) {
+                if ( doFree )
+                    rc = DosFreeModule( pHnd );
+                else
+                    rc = NO_ERROR;
+                if ( rc == NO_ERROR ) {
+                    map.remove( it );
+                    delete lib;
+                } else
+                    lib->refCount++;                
+            } else
+                rc = NO_ERROR;
+            break;
+        }
+    }
+    if ( rc == NO_ERROR )
+        pHnd = 0;
+#if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
+    else
+        qSystemWarning( "Failed to unload library!", rc );
+#endif
+    return rc == NO_ERROR;
+}
+
 #endif //QT_NO_LIBRARY
