Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    3rd Party Software
    3
    4
    565
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      filipdns last edited by filipdns

      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!

      .h

      #ifndef MAILNOTIFICATION_H
      #define MAILNOTIFICATION_H
      
      #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
      {
          Q_OBJECT
      
      public:
          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);
      
      private:
          QString username;
          QString password;
          static char *payload_text;
      };
      
      
      #endif // MAILNOTIFICATION_H
      
      

      .cpp

      #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');
          if(data)
          {
              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);
                  upload_ctx->lines_read++;
                  payload_text = data + 1;
                  //free(tt);
                  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");
          db.setDatabaseName("C:/Databases/4967df1f72da4cc275387bc5c81774de.sqlite");
          if(!db.open())
          {
              qDebug()<<"Problem opening database";
          }
          QSqlQuery query;
          query.prepare("SELECT nom, prenom FROM ident_log WHERE email ='Oui'");
          query.exec();
          //query.next();
      
          while (query.next()) {
          mail= query.value(1).toString().toLower()+"."+query.value(0).toString().toLower()+"@domaine.fr";
          if(mail!="")
          {
              this->recipients = curl_slist_append(this->recipients, this->mail.toStdString().c_str());}
          }
          db.close();
      
          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://mailex.domaine.fr:25");
          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);
          changeFrom(from);
          changeCc("filipdns@gmail.com");
          changeSubject(subject);
          changeMessage(message);
          resetParameters();
          setDate();
          setPayloadText();
          setAllCurlOptions();
      
          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);
            status=curl_easy_strerror(this->res);//QString::number(response_code);
            qDebug()<<"status c++ "<<status;
          }
          curl_slist_free_all(this->recipients);
          curl_easy_cleanup(curl);
      }
      
      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: <dcd7cb36-11db-487a-9f3a-e652a9668efd@rfcpedant.example.org>\r\n") + subject_str + QString("\r\n") + message_str;
          }else{
              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;
      }
      
      
      Pablo J. Rogina 1 Reply Last reply Reply Quote 0
      • Pablo J. Rogina
        Pablo J. Rogina @filipdns last edited by

        @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 :-)

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        F 1 Reply Last reply Reply Quote 3
        • F
          filipdns @Pablo J. Rogina last edited by

          @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

          Philippe

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @filipdns last edited by JonB

            @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?

            1 Reply Last reply Reply Quote 2
            • First post
              Last post