desktop app crashed when 2 event loops are used



  • I am trying to write some data to an url using get request from QNetworkAccessManager class. I am creating an event loop in it. There is another event loop running in the main thread which is the main event loop for the application. The application is getting crashed when it reaches eventLoop.exec(); line in the code below. Any idea why is it happening??

    {
    SharedPtr<QNetworkAccessManager> networkAccessManager;
    
    networkAccessManager(new QNetworkAccessManager());
    
    bool res = write(request);
    }
    
    bool Write(QNetworkRequest request) const
    {
        QEventLoop eventLoop;
        connect(networkAccessManager.get(), &QNetworkAccessManager::finished, &eventLoop, &QEventLoop::quit);
        int count = 0;
        while (count < retryCount)
        {
            QNetworkReply* reply = networkAccessManager->get(request);
            eventLoop.exec();
            if (reply != NULL && reply->error() == 0)
                return true;
            count++;
        }
        return false;
    }
    

    **networkAccessManager is the QNetworkAccessManager object.


  • Moderators

    @rahush

    Hi and welcome to devnet forum

    I have added code markers around your code.
    Please enclose your code snippets with code markers using right button "</>"above your edit window. This makes the code snippets easier to read.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    In your code, once your call .get and then ->get(). How can that compile ?

    You should also post the stack trace of your crash.



  • @SGaist

    SharedPtr<QNetworkAccessManager> networkAccessManager;

    networkAccessManager(new QNetworkAccessManager());

    networkAccessManager is not an object of QNetworkAccessManager, it is a SharedPtr.



  • @rahush I don't see any obvious reason for the crash.. Can you post the stack trace?



  • I've never used an event loop like this. How does the network manager know that it is supposed to post/receive events from the "new" event loop? I see no connection between the two objects.



  • The crash is probably from the connect line:

    connect(networkAccessManager.get(),...
    // should be ?
    connect(networkAccessManager,...)
    

    I don't think any of this would be necessary if you used the signals and slots to handle the events (as opposed to a loop waiting for a response). Just a thought.



  • What scope is this?

    {
    SharedPtr<QNetworkAccessManager> networkAccessManager;
    
    networkAccessManager(new QNetworkAccessManager());
    
    bool res = write(request);
    }
    
    

    You have declared networkAccessManager locally in that scope. What networkAccessManager are you referring to inside the Write method?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.