title: libcurl curl_easy_perform() crashes program when moved inside function
body: <p>Using Qt5 with the libcurl library, I try and download a directory listing from my local FTP server. If I keep all the curl_easy_setopt() and curl_easy_perform() in main(), it works perfectly. However if I try and put them all in a function for a single call, the program crashes when it gets to curl_easy_perform().</p>
  
  <p>I'm using libcurl version 7.40.0 - Win32 Generic 2000/XP</p>
  
  <pre><code>#include "mainwindow.h"
  #include &lt;QApplication&gt;
  #include &lt;curl/curl.h&gt;
  #include &lt;QDebug&gt;
  #include &lt;QFile&gt;
  
  struct FtpFile {
    const char *filename;
    FILE *stream;
  };
  
  size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
  {
    struct FtpFile *out=(struct FtpFile *)stream;
    if(out &amp;&amp; !out-&gt;stream) {
      /* open file for writing */
      out-&gt;stream=fopen(out-&gt;filename, "wb");
      if(!out-&gt;stream)
        return -1; /* failure, can't open file to write */
    }
    return fwrite(buffer, size, nmemb, out-&gt;stream);
  }
  
  int DownloadToFile(CURL *curl, FtpFile *file, bool ClearAfterWrite)
  {
      qDebug() &lt;&lt; "Before URL set";
      curl_easy_setopt(curl, CURLOPT_URL, "ftp://127.0.0.1");
      qDebug() &lt;&lt; "URL set";
  
      curl_easy_setopt(curl, CURLOPT_USERPWD, "maptester:sync");
      qDebug() &lt;&lt; "Login set";
  
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
      qDebug() &lt;&lt; "Write Function set";
  
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&amp;file);
      qDebug() &lt;&lt; "Write Data set";
  
      int Result = curl_easy_perform(curl);
      qDebug() &lt;&lt; "curl perform passed";
  
      if(ClearAfterWrite)
      {
          file-&gt;stream = NULL;
      }
  
      return Result;
  }
  
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
  
  
      FtpFile ftpfile =
      {
        "C:\\users\\Ir77\\Desktop\\ftpLS.txt", /* name to store the file as if succesful */
        NULL
      };
  
      curl_global_init(CURL_GLOBAL_DEFAULT);
  
      CURL *curl = curl_easy_init();
      qDebug() &lt;&lt; "easy init passed";
      if(curl)
      {
          int Result;
          qDebug() &lt;&lt; "curl check passed";
  #if 1
          Result = DownloadToFile(curl, &amp;ftpfile, true);
  #else
          curl_easy_setopt(curl, CURLOPT_URL, "ftp://127.0.0.1/");
  
          curl_easy_setopt(curl, CURLOPT_USERPWD, "maptester:sync");
  
          curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
  
          curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&amp;ftpfile);
  
          Result = curl_easy_perform(curl);
  
  #endif
  
        if(Result != CURLE_OK)
        {
            qDebug() &lt;&lt; "File Download error!";
        }
        else
        {
            qDebug() &lt;&lt; "File got something!";
        }
  
        /* always cleanup */
        curl_easy_cleanup(curl);
      }
  
      curl_global_cleanup();
  
  
      return a.exec();
  }
  </code></pre>