cURL / Mailing Lists / curl-library / Single Mail

curl-library

curl_multi_remove_handle() crashes if called before transfers are complete.

From: Saqib Ali <saqib.ali.75_at_gmail.com>
Date: Tue, 8 Mar 2011 19:01:50 -0500

I'm using the Multi-Interface.

When I try to remove an easy_handle from the multi's stack after the files
are transmitted, everything works fine. However, When I try to remove an
easy_handle from the multi's stack during file transmission it Seg Faults.

You can see the code below. I have labelled important blocks of code "BLOCK
A", "BLOCK B" and "BLOCK C" in the comments. In this code, I remove the Easy
Handle from the MultiStack after transmissions are done. It works fine.

However, if you change the code by moving Block C between Blocks A & B, you
will see that it gives a seg fault.

Suggestions?

- Saqib

#include "curl.h"
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>

using namespace std;

int main()
{
CURLMcode retVal;
 curl_global_init(CURL_GLOBAL_ALL);
CURLM* multiHandle = curl_multi_init();
CURL* newEasyHandle = curl_easy_init();
curl_easy_setopt(newEasyHandle, CURLOPT_USERPWD, "myUserName:myPassWord");
curl_easy_setopt(newEasyHandle, CURLOPT_UPLOAD, 1);
curl_easy_setopt(newEasyHandle, CURLOPT_VERBOSE, 1);
FILE* fd = fopen("MySourceFile", "r"); // open file to upload
curl_easy_setopt(newEasyHandle, CURLOPT_READDATA, fd);
curl_easy_setopt(newEasyHandle, CURLOPT_URL,
"sftp://MyRemoteIP/~/MyTargetFileName");
struct stat fileInfo;
stat("MySourceFile", &fileInfo);
curl_easy_setopt(newEasyHandle, CURLOPT_INFILESIZE, (long)fileInfo.st_size);

 // BLOCK A: Now add the easyHandle to the multiStack
curl_multi_add_handle(multiHandle, newEasyHandle);
int stillRunning, retValcm;
retVal = curl_multi_perform(multiHandle, &stillRunning);
if (retVal != CURLM_OK)
cout << "curl_multi_perform() failed!" << endl;
// END BLOCK A

 // BLOCK B: Wait until the transfers are done.
while (stillRunning > 0)
{
sleep(1);
retValcm = curl_multi_perform(multiHandle, &stillRunning);
cout << "stillRunning = " << stillRunning << " curl_multi_perform() == " <<
retValcm << endl;
}
// END BLOCK B

 // BLOCK C: Now remove the easyHandle from the multiStack
sleep(2);
retVal = curl_multi_remove_handle(multiHandle, newEasyHandle);
if (retVal == CURLM_OK)
cout << "curl_multi_remove_handle() was successfull!" << endl;
else
cout << "curl_multi_remove_handle() failed!" << endl;
// END BLOCK C
 return 0;

}

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-03-09