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.
GNU Lesser General Public License v2.1
213 stars 64 forks source link

Blocking code crashes after code block finishes part 2 #83

Closed joshorenberg closed 1 year ago

joshorenberg commented 2 years ago

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

joshorenberg commented 2 years ago

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...

joshorenberg commented 2 years ago

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.

joshorenberg commented 2 years ago

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: https://github.com/bluetiger9/SmtpClient-for-Qt/issues/123

ghorwin commented 1 year ago

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.

dantti commented 1 year ago

Sender is async you are just deleting it before it has a chance to work.

dantti commented 1 year ago

blocking mode is unsupported on v3