QWebEngine process termination issue



  • Hi! I want to make QWebEngine access urls which requires the authentication. I have downloaded the simplebrowser as the example, and when I run http://192.168.0.1, it display the authentication dialog, but when I canceled and closed it, the simplebrowser displays issue:

    [11784:13720:0810/193655.735:ERROR:process_win.cc(135)] Unable to terminate process: Access is denied. (0x5)

    Code:

    void WebPage::handleAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth)
    {
        QWidget *mainWindow = view()->window();
        QDialog dialog(mainWindow);
        dialog.setModal(true);
        dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
    
        Ui::PasswordDialog passwordDialog;
        passwordDialog.setupUi(&dialog);
    
        passwordDialog.m_iconLabel->setText(QString());
        QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow));
        passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
    
        QString introMessage(tr("Enter username and password for \"%1\" at %2")
                             .arg(auth->realm()).arg(requestUrl.toString().toHtmlEscaped()));
        passwordDialog.m_infoLabel->setText(introMessage);
        passwordDialog.m_infoLabel->setWordWrap(true);
    
        if (dialog.exec() == QDialog::Accepted) {
            auth->setUser(passwordDialog.m_userNameLineEdit->text());
            auth->setPassword(passwordDialog.m_passwordLineEdit->text());
        } else {
            // Set authenticator null if dialog is cancelled
            *auth = QAuthenticator(); // possible memory leak
        }
    }
    

    The same issue is also present in my project. During my simplebrowser testing I have noticed that such issue is only available when the dialog has been canceled. So is it memory leak? Thanks.


  • Moderators

    *auth = QAuthenticator(); // possible memory leak

    It could leak but may not depending on how auth was allocated. It's definitely not good coding. And it could cause an issue if auth is then cleaned up with a delete or something. You should never modify what a pointer points to inside a function like that. Ownership of pointers should have a clear chain. The caller should own that pointer and it's memory.

    It it was allocated with new and has no parent object then it will leak. If it's on the stack or has a parent it will be fine.

    It's a very weird way to do it though. A bool return or an int or pretty much anything to indicate failure would be way better than messing with a pointer like that.



  • @ambershark

    Hi! Yes, I know it's weird. I tried to set auth = nullptr; but the dialog never closes. When I delete the object - delete auth; it crashes. QAuthenticator doesn't have deleteLater() method.

    Also I have tried detach method but still it never closes.

    This code is the only way to close the dialog: *auth = QAuthenticator(); but it will cause the [11784:13720:0810/193655.735:ERROR:process_win.cc(135)] Unable to terminate process: Access is denied. (0x5) issue. So how to fix it?


  • Moderators

    @Cobra91151 Oh I see, you are having issues with the window not closing?

    Also you don't want to mess with auth pointer at all. Like I said it's expected to be handled outside that function.

    The window should be closed by the time you get to that else {} clause though. If it's not closing that is concerning. Can you show a video of it happening?



  • @ambershark

    I have changed the code.

    if (dialog.exec() == QDialog::Accepted) {
            auth->setUser(passwordDialog.m_userNameLineEdit->text());
            auth->setPassword(passwordDialog.m_passwordLineEdit->text());
     } else {
            // Set authenticator null if dialog is cancelled
            //*auth = QAuthenticator();
            auth->setUser(QString());
            auth->setPassword(QString());
            auth = nullptr;
     }
    

    I will create the gif animation to illustrate the issue.



  • @ambershark

    So here is the issue when dialog never closes:
    Issue

    Also, when else clause is removed from the code, the same issue is still present. By the way, I can't set the parent to QAuthenticator because it only accepts const QAuthenticator &other as the parent and also default constructor is available. I think that I will try to re-implement it, to add some custom methods/constructors to the class.


  • Moderators

    @Cobra91151 Ok that makes a lot more sense.. So the dialog is indeed closing but it is immediately coming back up since the function is called again right away to get authentication.

    So along those lines what is calling handleAuthenticationRequired? What you want to do is only call that once and then maybe proceed to an 403 page when it fails rather than continually calling it.



  • @ambershark

    It connects here in simplebrowser project:

    WebPage::WebPage(QWebEngineProfile *profile, QObject *parent)
        : QWebEnginePage(profile, parent)
    {
        connect(this, &QWebEnginePage::authenticationRequired, this, &WebPage::handleAuthenticationRequired);
        connect(this, &QWebEnginePage::proxyAuthenticationRequired, this, &WebPage::handleProxyAuthenticationRequired);
    }
    

    When *auth = QAuthenticator(); is set and dialog has been canceled I got:

    0_1533975552605_2018-08-11_111828.png



  • Also I have noticed that the process exits normal, maybe the leak is in my code. I will check and reply later.



  • So my code:

     connect(browser->page(), &QWebEnginePage::authenticationRequired, [this](const QUrl &url, QAuthenticator *authenticator) {
            WebAuthenticator *webAuthDialog = new WebAuthenticator(browser->page()->view()->window());
            connect(this, &Test::authInfo, webAuthDialog, &WebAuthenticator::setAuthInfo);
            connect(webAuthDialog, &WebAuthenticator::authenticationData, [this, authenticator, webAuthDialog](QString user, QString password) {
                if (!user.isEmpty() || !password.isEmpty()) {
                    authenticator->setUser(user);
                    authenticator->setPassword(password);
                } else {
                    QMessageBox::critical(this, QObject::tr("Error"), QObject::tr("Please enter your credentials to sign in!"), QMessageBox::Ok);
                }
    
                webAuthDialog->close();
                webAuthDialog->deleteLater();
            });
            connect(webAuthDialog, &WebAuthenticator::authenticationCanceled, [this, authenticator, webAuthDialog]() {
                webAuthDialog->close();
                webAuthDialog->deleteLater();
                *authenticator = QAuthenticator();
                qDebug() << "Test";
            });
            emit authInfo(url, authenticator, qApp->style()->standardIcon(QStyle::SP_MessageBoxQuestion));
            webAuthDialog->exec();
        });
    

    WebAuthenticator is the GUI dialog, it has setAttribute(Qt::WA_DeleteOnClose); so it will be deleted when closed. The code leads to the issue: [13628:8812:0811/120557.720:ERROR:process_win.cc(135)] Unable to terminate process: Access is denied. (0x5)
    Any ideas how to fix it? Thanks.



  • This issue [13628:8812:0811/120557.720:ERROR:process_win.cc(135)] Unable to terminate process: Access is denied. (0x5) is only in debug mode. Maybe it's the default behavior? Thanks.



  • So, my code works, I have tried it on the different project. The problem is, when some leak/issue was occurred in the QWebEngine process, it will fail to close the process and display the issue: [13628:8812:0811/120557.720:ERROR:process_win.cc(135)] Unable to terminate process: Access is denied. (0x5) even when the leak/issue has been fixed.



  • Yes, I was right. I have deleted the cache and QWebEngine directory from the local dir repository, shut down the PC for about 3 hours, then booted, started the project and checked the issue. Now the QWebEngine process exits successfully sometimes in debug mode. I think the issue is resolved. Thank you.


Log in to reply
 

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