cutelyst / simple-mail

An SMTP library written in C++ for Qt. Allows applications to send emails (MIME with text, html, attachments, inline files, etc.) via SMTP. Supports SSL and SMTP authentication.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blocking code crashes after code block finishes part 2

joshorenberg opened this issue · comments

Hi I have the same problem as issue #80 after modifying the code as mentioned. By no means do I mean to rush you to fix this; please do on your own time, I just thought you didn't see my other post. Thanks! Backtrace on bottom.

void Program::sendEmails()
{
    QMultiMap<QString,BackupJob>::const_iterator iterator = emailAddressMultiMap.constBegin();
    while (iterator != emailAddressMultiMap.constEnd())
    {
        messageString += ... //construct the string here

        SimpleMail::Sender sender("smtp.***.com", 465, SimpleMail::Sender::SslConnection);

        sender.setUser("address@***.com");
        sender.setPassword("password");

        SimpleMail::MimeMessage message;
        message.setSender(SimpleMail::EmailAddress("from@from.com", "Program"));
        message.addTo(SimpleMail::EmailAddress(iterator.key()));
        message.setSubject("Email");

        auto text = new SimpleMail::MimeText;
        text->setText(messageString);
        message.addPart(text);

        sender.sendMail(message); // Blocks untill mail is delivered or errored
        //qDebug() << sender.responseText();
        sender.quit();

        QString emailAddress = iterator.key();
        while (iterator != emailAddressMultiMap.constEnd() && iterator.key() == emailAddress)
            ++iterator;
    }
}

backtrace cl2

I found a workaround for the error... I offloaded the email sending code to a worker thread (which is what I wanted in the first place) and everything is working now, no more crashes. Weird...

It's back! I merged in the email code branch and am getting the same crash now even in the worker thread. However if I run using the simplemail .lib file built in debug mode instead of release mode and run the program in debug mode the program doesn't crash.

And another possibly important piece of information... I tried using Bluetiger9 for email as well (another email library for Qt) and got a very similar crash with an almost identical backtrace. See here: bluetiger9/SmtpClient-for-Qt#123

I suggest you look at the lifetime of your objects! Sending an email is an asynchronous operation. You must not (implicitely) delete memory while in an event loop. It is likely that while you wait for your mail to be delivered, some other event kicks in (and is processed) that tempers with your memory. The SmtpClient4Qt works fine with a similar code, btw.