Implicitly loaded library isn't unloaded at program termination when loading wintab32.dll and calling WTInfoW

152 Views Asked by At

Original Post

I have a Qt application. This application needs to call some function in a dynamic library that is loaded implicitly. In the dynamic library, there is one global variable that is created when the dll is loaded and destroyed when it is unloaded.

Here's the code:

#include <QApplication>

#include <base/BASE_TEST.h>

int main(int qargc, char** qargv)
{
  QApplication application(qargc, qargv);

  BASE_TEST::myDLLFunction(); // call to a function in an implicitly loaded dynamic library.

  return 0;
}

Implementation of myDLLFunction and of the private class of the global object.

#include <base/BASE_TEST.h>

#include <stdio.h>

class MyTest
{
public:
  MyTest() { printf("------------------------------\nTEST BEGIN\n------------------------------\n"); }
  ~MyTest() { printf("------------------------------\nTEST END\n------------------------------\n"); }
};

MyTest test; // created at the library's loading

void BASE_TEST::myDLLFunction()
{
  printf("Call from dynamic library\n");
}

If I run the application, here's what being printed in the command prompt:

------------------------------
TEST BEGIN
------------------------------
Call from dynamic library
------------------------------
TEST END
------------------------------

Up to here all is well. However, if I retrieve some information about the number of screens using QApplication::desktop(), the global object of the dynamic library isn't destroyed.

int main(int qargc, char** qargv)
{
  QApplication application(qargc, qargv);
  QDesktopWidget* desktop = QApplication::desktop(); // This call prevent the global objects to be destroyed.

  BASE_TEST::myDLLFunction(); // call to a function in an implicitly loaded dynamic library.

  return 0;
}

Here's what is printed in the command prompt:

------------------------------
TEST BEGIN
------------------------------
Call from dynamic library

The main function still returns normally and no exception is thrown.

I looked at the code of QApplication and QDesktopWidget and the QDesktopWidget destructor is being called at the end of the main function's scope and QDesktopWidgetPrivate::cleanup() is called.

I'm on Windows, using Qt 4.8.6.

Does someone has any idea? Thanks! :)

Edit

As mentionned in the answer below, the problem seems to be linked to loading wintab32.dll which will load the Wacom driver's dynamic library if installed.

1

There are 1 best solutions below

0
On

I finally found the source of the issue:

Calling QApplication::desktop() made Wacom_Tablet.dll be loaded. By uninstalling the Wacom driver, the problem went away.

I was able to reduce the sample program to:


#include "../baseTest/BASE_TEST.h"

#include <wtypes.h>
#include "wintab.h"

typedef UINT(WINAPI *PtrWTInfo)(UINT, UINT, LPVOID);

static PtrWTInfo ptrWTInfo = 0;

int main(int /*qargc*/, char** /*qargv*/)
{
  BASE_TEST::myDLLFunction(); // call to a function in an implicitly loaded dynamic library.

  HMODULE hWintab = LoadLibrary(L"wintab32.dll");

  PtrWTInfo pWTInfo = (PtrWTInfo)GetProcAddress(hWintab, "WTInfoW");

  WORD thisVersion;
  pWTInfo(WTI_INTERFACE, IFC_SPECVERSION, &thisVersion);

  if (hWintab)
    FreeLibrary(hWintab);

  return 0;
}

and still be able to reproduce the issue.

I've contacted Wacom about it and am waiting their reply.