I load my qt-dll by button clicking:
Qt Code:
  1. void CMFCTestDlg::OnBnClickedButton1()
  2. {
  3. HINSTANCE hinstLib;
  4. MYPROC ProcAdd;
  5. BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
  6.  
  7. // Get a handle to the DLL module.
  8.  
  9. hinstLib = LoadLibrary(TEXT("qttest.dll"));
  10.  
  11. // If the handle is valid, try to get the function address.
  12.  
  13. if (hinstLib != NULL)
  14. {
  15. ProcAdd = (MYPROC) GetProcAddress(hinstLib, "createWindow");
  16.  
  17. // If the function address is valid, call the function.
  18.  
  19. if (NULL != ProcAdd)
  20. {
  21. fRunTimeLinkSuccess = TRUE;
  22. char * params[] = {"1"};
  23. (ProcAdd) (1, params);
  24. }
  25.  
  26. // Free the DLL module.
  27.  
  28. fFreeResult = FreeLibrary(hinstLib);
  29. }
  30.  
  31. // If unable to call the DLL function, use an alternative.
  32.  
  33. if (! fRunTimeLinkSuccess)
  34. MessageBox(L"Message printed from executable\n");
  35. }
To copy to clipboard, switch view to plain text mode 

Function createWindow() is as follows:
Qt Code:
  1. extern "C" __declspec(dllexport) int createWindow(int argc, char *argv[])
  2. {
  3. new QApplication(argc, argv);
  4. Q_CHECK_PTR( qApp );
  5.  
  6. MainWindow* w = MainWindow::getInstance();
  7. qApp->setMainWidget(w);
  8. w->show();
  9.  
  10. return qApp->exec();
  11. }
To copy to clipboard, switch view to plain text mode 

When I click button then MainWindow appears, I close that window and then I click button again -- raising up assert: qcoreapplication: there should be only one application object...

Then I wrote the following code:
Qt Code:
  1. void MainWindow::closeEvent(QCloseEvent * event)
  2. {
  3. qApp->exit();
  4. delete qApp;
  5. }
To copy to clipboard, switch view to plain text mode 
But now i'm getting error in _CrtIsValidHeapPointer method after second closing of MainWindow.

How to unload qt-dll correctly?