I have a problem with copying the content of a structure.. following situation...I have a struct arg_struct in my class Session:
struct Session::arg_struct
{
const char* targetFilePath;
const char* url;
unsigned int thread_id;
Session::ThreadFinishedCallbackFunction callback;
};
and in one of my method I start a thread and give the struct to the function that will be executed:
{
...
arg_struct args;
args.targetFilePath = targetFilePath;
args.url = req_url;
args.thread_id = ++mThread_id;
args.callback = callback;
curl_global_init(CURL_GLOBAL_ALL);
error = pthread_create(&thread,NULL,download,&args);
}
Now the download function will be executed:
void* download(void* arguments)
{
Session::arg_struct ar = *(Session::arg_struct*) arguments;
Session::arg_struct args;
args.targetFilePath = new char[strlen(ar.targetFilePath)];
args.url = new char[strlen(ar.url)];
strcpy(const_cast<char*>(args.targetFilePath),ar.targetFilePath);
strcpy(const_cast<char*>(args.url),ar.url);
args.callback = ar.callback;
args.thread_id = ar.thread_id;
cout << "copied" << endl;
CURL *curl;
FILE* datafile;
datafile = fopen(args.targetFilePath, "w");
if(datafile != NULL)
{
curl = curl_easy_init();
curl_easy_setopt(curl,CURLOPT_URL,args.url);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,datafile);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(datafile);
} else {
cout << "error: failed to open file!" << endl;
return NULL;
}
At the beginning of the download function I copy the struct (I quess ;) ) but I got the error that the file can not be opened. The problem is that when I try to open the file with fopen() the struct is empty. It seems that I havent copied the struct correctly or the pointer to the struct (void* arguments) is allready not useable. Have you got any suggestions?