QWebEngine process termination issue
-
wrote on 10 Aug 2018, 16:48 last edited by
Hi! I want to make
QWebEngine
access urls which requires the authentication. I have downloaded thesimplebrowser
as the example, and when I run http://192.168.0.1, it display the authentication dialog, but when I canceled and closed it, thesimplebrowser
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. -
wrote on 10 Aug 2018, 21:01 last edited by
*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 ifauth
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.
-
*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 ifauth
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.
wrote on 10 Aug 2018, 21:09 last edited by Cobra91151 8 Oct 2018, 21:15Hi! 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 havedeleteLater()
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? -
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 havedeleteLater()
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?wrote on 10 Aug 2018, 21:15 last edited by@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? -
@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?wrote on 10 Aug 2018, 21:22 last edited byI 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.
-
@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?wrote on 10 Aug 2018, 21:28 last edited by Cobra91151 8 Oct 2018, 21:44So here is the issue when dialog never closes:
Also, when
else
clause is removed from the code, the same issue is still present. By the way, I can't set the parent toQAuthenticator
because it only acceptsconst 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. -
So here is the issue when dialog never closes:
Also, when
else
clause is removed from the code, the same issue is still present. By the way, I can't set the parent toQAuthenticator
because it only acceptsconst 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.wrote on 10 Aug 2018, 22:23 last edited by@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. -
@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.wrote on 11 Aug 2018, 08:26 last edited byIt 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: -
wrote on 11 Aug 2018, 08:27 last edited by
Also I have noticed that the process exits normal, maybe the leak is in my code. I will check and reply later.
-
wrote on 11 Aug 2018, 09:07 last edited by Cobra91151 8 Nov 2018, 09:34
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 hassetAttribute(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. -
wrote on 11 Aug 2018, 09:37 last edited by
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. -
wrote on 11 Aug 2018, 10:06 last edited by Cobra91151 8 Nov 2018, 10:07
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. -
wrote on 11 Aug 2018, 13:06 last edited by
Yes, I was right. I have deleted the
cache
andQWebEngine
directory from the local dir repository, shut down thePC
for about 3 hours, then booted, started the project and checked the issue. Now theQWebEngine
process exits successfully sometimes in debug mode. I think the issue is resolved. Thank you.
1/13