app crash one time on ten using libcurl without any debug info...

  • Hello, my project have to send mail during night but one time on ten, that make the app crash without any information or error to know why...

    Could you help me to found what I did wrong?

    Thanks a lot!


    #include <QDebug>
    #include <curl/curl.h>
    #include <iostream>
    #include <ctime>
    #include <string>
    #include <string.h>
    #include <cstddef>
    using namespace std;
    struct upload_status {
        int lines_read;
    class mailNotification : public QObject
        QString from;
        QString to;
        QString cc;
        QString subject;
        QString message;
        QString date;
        QString mail;
        QString xxx;
        long response_code;
        struct upload_status upload_ctx;
        QString status;
        CURL *curl;
        CURLcode res;
        struct curl_slist *recipients;
        void resetParameters();
        void setPayloadText();
        void setDate();
        static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp);
        void setUser(QString username, QString password);
        void setAllCurlOptions();
        Q_INVOKABLE void sendEmail(QString  username, QString  password, QString  from, QString  subject, QString  message);    
        Q_INVOKABLE QString returnmessage();
        void changeFrom(QString f);
        void changeTo(QString t);
        void changeCc(QString c);
        void changeSubject(QString s);
        void changeMessage(QString m);
        QString username;
        QString password;
        static char *payload_text;


    #include <QSqlDatabase>
    #include <QtSql>
    #include "mailnotification.h"
    char* mailNotification::payload_text = NULL;
    void mailNotification::resetParameters()
        this->res = CURLE_OK;
        this->recipients = NULL;
        this->upload_ctx.lines_read = 0;
        //qDebug() << "init curl done";
    void mailNotification::setDate()
        time_t now = time(0);
        this->date = ctime(&now);
        //qDebug() << "date";
    size_t mailNotification::payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
        struct upload_status *upload_ctx = (struct upload_status *)userp;
        char *data;
        char *tt;
        if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
            return 0;
        data = strchr(payload_text, '\n');
            size_t pSize = strlen(payload_text);
            size_t dSize = strlen(data);
            size_t diff = pSize-dSize;
            tt = (char*)malloc((diff + 1) * sizeof(char));
            strncpy(tt, payload_text, diff+1);
            tt[diff+1] = '\0';
            if(tt) {
                size_t len = strlen(tt);
                memcpy(ptr, tt, len);
                payload_text = data + 1;
                return len;
        return 0;
    void mailNotification::setUser(QString username, QString password)
        this->username = username;
        this->password = password;
    void mailNotification::setAllCurlOptions()
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase("QSQLITE");
            qDebug()<<"Problem opening database";
        QSqlQuery query;
        query.prepare("SELECT nom, prenom FROM ident_log WHERE email ='Oui'");
        while ( {
        mail= query.value(1).toString().toLower()+"."+query.value(0).toString().toLower()+"";
            this->recipients = curl_slist_append(this->recipients, this->mail.toStdString().c_str());}
        curl_easy_setopt(this->curl, CURLOPT_USERNAME, this->username.toStdString().c_str());
        curl_easy_setopt(this->curl, CURLOPT_PASSWORD, this->password.toStdString().c_str());
        curl_easy_setopt(this->curl, CURLOPT_MAIL_FROM, this->from.toStdString().c_str());
        curl_easy_setopt(this->curl, CURLOPT_MAIL_RCPT, this->recipients);
        curl_easy_setopt(this->curl, CURLOPT_URL, "smtp://");
        curl_easy_setopt(this->curl, CURLOPT_UPLOAD, 1L);
        if(this->cc != ""){
            this->recipients = curl_slist_append(this->recipients, this->cc.toStdString().c_str());
        curl_easy_setopt(this->curl, CURLOPT_READFUNCTION, this->payload_source);
        curl_easy_setopt(this->curl, CURLOPT_READDATA, &upload_ctx);
        curl_easy_setopt(this->curl, CURLOPT_UPLOAD, 1L);
        curl_easy_setopt(this->curl, CURLOPT_VERBOSE, 1L);
    void mailNotification::sendEmail(QString username, QString  password, QString from, QString subject, QString message)
        this->curl = curl_easy_init();
        setUser(username, password);
        this->res = curl_easy_perform(this->curl);
        /* Check for errors */
        if(this->res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(this->res));
        if(res == CURLE_OK) {
          curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
          qDebug()<<"status c++ "<<status;
    void mailNotification::changeFrom(QString f)
        this->from = f;
    void mailNotification::changeTo(QString t)
        this->to = t;
    void mailNotification::changeCc(QString c)
        this->cc = c;
    void mailNotification::changeSubject(QString s)
        this->subject = s;
    void mailNotification::changeMessage(QString m)
        this->message = m;
    void mailNotification::setPayloadText()
        payload_text = NULL;
        QString to_str = "To: <" +this->to+ ">\r\n";
        QString from_str = "From: <" +this->from+ ">\r\n";
        QString cc_str = "Cc: <" + this->cc + ">\r\n";
        QString date_str = "Date: " + this->date;
        QString subject_str = "Subject: " + this->subject +"\r\n";
        QString message_str = this->message + "\r\n";
        //qDebug() << "message";
        if(this->cc == ""){
            xxx = date_str + to_str + from_str + QString("Message-ID: <>\r\n") + subject_str + QString("\r\n") + message_str;
            xxx = date_str + to_str + from_str + cc_str + subject_str + QString("\r\n") + message_str;
        payload_text = (char*)malloc(xxx.length()*sizeof(char*));
        strcpy(payload_text ,xxx.toStdString().c_str());
        //qDebug() << "payload c++ "<<payload_text;
        //return payload_text;
    QString mailNotification::returnmessage()
    return status;

  • @filipdns have you run Valgrind on your application to spot for memory leaks?

    All those structs and pointers are claiming for something bad to happen if not treated well :-)

  • @Pablo-J.-Rogina
    Hello, sorry I don't know Valgrind and I don't know what you mean about "All those structs and pointers are claiming for something bad to happen if not treated well "

    May be do you have simplest solution to send email with Qt?

    Kind regards


  • @filipdns
    Have you run your program under the debugger and looked at the traceback when whatever you mean by "app crash" occurs? Are you under Linux with core dumps? That is always by far the best place to start from.

    If no core dumps and you can't run under debugger (e.g. it runs unattended overnight), what about putting in extensive but basic logging lines (to file) to see where it's going?

Log in to reply