QtService icon indicating copy to clipboard operation
QtService copied to clipboard

QCoreApplication::instance() returns a null in application.

Open stephenlang84 opened this issue 4 years ago • 2 comments

I have been encountering a problem when testing servicebackend on Windows. I built the QtService project independently by the same Kit used for my project then copy the libs and plugin to my project.

My problem is after built and loaded the servicebackends plugin then create and run my service successfully (i can see the status of service is running and got logs). but checking QCoreApplication object, I see QCoreApplication object that plugin created isn't the object that i get in my project when using the function static QCoreApplication::instance(), on my project it always is null. so my project can't use event loop.

Any comments on that? Thanks you.

stephenlang84 avatar Apr 08 '20 05:04 stephenlang84

Huh. That's strange. I create that instance myself in the plug in. Maybe you are checking to early? Please show me your service code

Skycoder42 avatar Apr 11 '20 05:04 Skycoder42

There are my source:

class ECSyncService : public QtService::Service
{
public:
    explicit ECSyncService(int &argc, char **argv) :
        Service(argc, argv)
    {
    }

    ~ECSyncService()
    {
        // we must reset handler to avoid crash when logging after _loggerThread exits
        qInstallMessageHandler(nullptr);
        _loggerThread.quit();
        _loggerThread.wait();
    }
protected:
    CommandResult onStart() override
    {
        qInfo() << "Service was started";
		qDebug() << QCoreApplication::instance(); // it returns a null.
        //    configure the logger
        Log::Logger::getDefault()->setup();
        qInstallMessageHandler(&Log::Logger::messageHandler);
        Log::Logger::getDefault()->moveToThread(&_loggerThread);
        _loggerThread.start(QThread::Priority::LowPriority);

        _service.reset(new CSManager());
        connect(_service.data(), &CSManager::stopped, this, &Service::stopped);

        QMetaObject::invokeMethod(_service.data(), &CSManager::start);
        return CommandResult::Completed;
    }

    CommandResult onStop(int &exitCode) override
    {
        qInfo() << "Stop received";
        _service->stop();
        return CommandResult::Pending;
    }

private:
    QScopedPointer<CSManager> _service;
    QThread _loggerThread;
};

stephenlang84 avatar Apr 13 '20 02:04 stephenlang84