を使用してファイルをダウンロードしようとすると問題が発生しますlibcurl
。プログラムは複数のスレッドで動作し、ファイルをダウンロードする必要があるすべてのスレッドは、操作するlibcurl
ハンドルを作成します。
URL が正しい場合はすべて機能しますが、URL に誤りがあるとプログラムがクラッシュします。デバッグ モードでは、URL が正しくない場合curl_easy_perform
、エラー接続コードが返され、プログラムは動作します。対照的に、リリース時にクラッシュします。
このエラーを修正するにはどうすればよいですか?
ファイルをダウンロードするために使用するコードは次のとおりです。無関係なコードは抑制されています。
LoadFileFromServer
(
string& a_sURL
)
{
string sErrorBuffer;
struct DownloadedFile updateFile = { sFilenameToWrite, // name to store the local file if succesful
NULL }; // temp buffer
CURL* pCurl = curl_easy_init();
curl_easy_setopt( pCurl, CURLOPT_URL, a_sURL.data() );
curl_easy_setopt( pCurl, CURLOPT_FOLLOWLOCATION, 1L );
curl_easy_setopt( pCurl, CURLOPT_ERRORBUFFER, sErrorBuffer );
curl_easy_setopt( pCurl, CURLOPT_WRITEFUNCTION, BufferToFile );
curl_easy_setopt( pCurl, CURLOPT_WRITEDATA, &updateFile );
curl_easy_setopt( pCurl, CURLOPT_NOPROGRESS, 0 );
curl_easy_setopt( pCurl, CURLOPT_CONNECTTIMEOUT, 5L );
CURLcode res = curl_easy_perform( pCurl );
curl_easy_cleanup( pCurl );
}
int BufferToFile
(
void * a_buffer,
size_t a_nSize,
size_t a_nMemb,
void * a_stream
)
{
struct DownloadedFile *out = ( struct DownloadedFile * ) a_stream;
if( out && !out->stream )
{
// open file for writing
if ( 0 != fopen_s( &( out->stream ), out->filename.c_str(), "wb" ) )
return -1;
if( !out->stream )
return -1; /* failure, can't open file to write */
}
return fwrite( a_buffer, a_nSize, a_nMemb, out->stream );
}