S
S
Sergey Konovalov2015-01-20 10:25:37
Qt
Sergey Konovalov, 2015-01-20 10:25:37

Dll + QThread + QProcess = Heap corruption?

There is Dll in which Qthread is created. A certain class is moved to Qthread, inside which QProcess is used to launch an external 7z console application (for unpacking archives). All this leads to Heap corruption.
If you replace the use of the external program 7z with QZipReader, for example, then everything is fine.
Actually the question is: is it correct to use such a bunch of Dll + QThread + QProcess from the point of view of Qt? If so, what is the right way to do it in order to insure yourself against Heap corruption?
PS system Windows 7 x64, Qt 5.3.2 (mingw482_32)
PPS
Qt in dll is initialized like this:
//*.h

#pragma once

#include <QThread>
#include <QtCore/QMutex>
#include <QApplication>
#include <memory>

class LibrarySingleton : public QThread
{
  Q_OBJECT

public:
  static LibrarySingleton* Instance();
  void close();
  ~LibrarySingleton();

protected:
  LibrarySingleton();
  virtual void run();

private:
  std::unique_ptr<QApplication> application;

  static std::unique_ptr<LibrarySingleton> instance;
  static QMutex lock;
};

//*.cpp
#include "librarysingleton.h"

QMutex LibrarySingleton::lock;
std::unique_ptr<LibrarySingleton> LibrarySingleton::instance;

LibrarySingleton* LibrarySingleton::Instance()
{
  if (!instance)
  {
    lock.lock();
    if (!instance)
      instance = std::unique_ptr<LibrarySingleton> (new LibrarySingleton);
    lock.unlock();
  }

  return instance.get();
}

LibrarySingleton::LibrarySingleton()
{
  if (QCoreApplication::instance() == nullptr) /// !!!
  {
    int argc = 1;
    char arg1[] = "library";
    char* argv[1] = {arg1};
    application = std::unique_ptr<QApplication>(new QApplication(argc, argv));
  }
}

void LibrarySingleton::run()
{
  if (application)
      application->exec();
}

void LibrarySingleton::close()
{
  if (application)
  {
    application.reset();
    quit();
    wait();
  }

  instance.reset();
}

When initializing my Dll, I do this:
LibrarySingleton::Instance()->start();
QThread is created like this:
_workThread.reset(new WorkThread(inputPath,targetPath));
QThread* thread = new QThread;
_workThread->moveToThread(thread);

connect(thread, SIGNAL(started()),	_workThread.get(), SLOT(process()));	
connect(_workThread.get(), SIGNAL(finished()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();

QProcess is used like this:
void WorkThread::process()
{
        //.....
  QProcess *archiveProcess = new QProcess(this);
  archiveProcess->start(utils.at(flagUtil)._pathExe, utils.at(flagUtil)._params);
  archiveProcess->waitForFinished();
  archiveProcess->close();
  delete archiveProcess;
       //.....
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2015-01-20
@gbg

Connecting to a program not based on Qt DLL with Qt is acceptable, and even works.
The problem is either in the link settings (different attitude to the use of the stack in the calling program and in the library), or in the incorrect initialization of Qt in Dll.
Well, another question is the degree of antiquity of the version of Qt used.

D
DancingOnWater, 2015-01-20
@DancingOnWater

Yes, correct.
That's just why it arose is impossible to say without additional information.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question