Delete session cookies qt

0

I have a qt application where I am storing all the session cookies in cookieJar. In order to delete the session even without closing the application, I am currently calling cookieJar->deleteLater(); when a button is pressed.

This is throwing the following error: The inferior stopped because it triggered an exception. Stopped in thread 0 by: Exception at 0x54d6559d, code: 0xc0000005: write access violation at: 0x1, flags=0x0 (first chance)

The cookieJar is currently a public variable and it was initialized with the current class as the parent. Why am I still unable to delete it?

c++
qt
session
cookies
qnetworkaccessmanager
asked on Stack Overflow Oct 1, 2014 by BurninatorDor

2 Answers

0

You don't need to destroy the QNetworkCookieJar, because:

Note: QNetworkAccessManager takes ownership of the cookieJar object.

Deleting of QNetworkCookieJar instance will cause accessing bad pointer by QNetworkAccessManager.

You can implement following to delete all cookies entries:

foreach (QNetworkCookie& cookie, networkAccessManager->cookieKar()->allCookies())
{
    networkAccessManager->cookieKar()->deleteCookie(cookie);
}
answered on Stack Overflow Oct 1, 2014 by Max Go • edited Oct 1, 2014 by Max Go
0

To do this for the general case, you're probably gonna have to subclass it, so you can access allCookies() or setAllCookies().

I wound up with this mess, to clear them after a network request:

void YahooCookieTest::replyReady(QNetworkReply *reply)
{
    QList<QNetworkCookie> cookies =
      reply->header(QNetworkRequest::SetCookieHeader).value< QList<QNetworkCookie> >();

    // read cookies if desired

    foreach (const QNetworkCookie &c, cookies)
        namNetAccessManager.cookieJar()->deleteCookie(c);

    // etc. ...

These cookiejar structures are especially hard to work with. You aren't allowed to get the value() of a header without a template cast. There's no method to empty the cookie jar for you. You can't even get at all the cookies in the cookie jar, without subclassing it. You can set cookies with a QString, but you can't access them as QStrings. I like Qt very well; but I feel this particular corner of it is bad API design. To prevent unnecessary subclassing, it seems to me, all ya gotta do, is make fewer things protected!

I've tried this only on the case of a single cookie. I think it'll work on more; but I'm making no promises.

answered on Stack Overflow Feb 18, 2019 by CodeLurker • edited Feb 18, 2019 by CodeLurker

User contributions licensed under CC BY-SA 3.0