찾다

 >  Q&A  >  본문

c++ - mfc中调用curl_slist_free_all时报错:读取位置 0x3d646e65 时发生访问冲突

环境:Win7 64位+VS2008
问题:在mfc中进行libcurl编程的时候,发现无法释放发送给服务端的header信息

问题代码如下

//释放资源
curl_slist_free_all(headers);

报错:0x586244d6 处最可能的异常: 0xC0000005: 读取位置 0x3d646e65 时发生访问冲突

完整代码

ReturnInfo CCSDNDlg::LoginServer(CString strUser,CString strPass)
{
    ReturnInfo returnInfo;
    returnInfo.bReturn = FALSE;

    CURL *curl;
    CURLcode res;
    struct curl_slist *headers = NULL;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    if(curl){
        //初始化cookie引擎
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
        curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION, 1L);

        //http请求头
        headers = curl_slist_append(headers,"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); //模拟浏览器
        headers = curl_slist_append(headers,"Host:passport.csdn.net");
        headers = curl_slist_append(headers,"Accept:*/*");
        headers = curl_slist_append(headers,"Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
        headers = curl_slist_append(headers,"Accept-Encoding:gzip, deflate");
        headers = curl_slist_append(headers,"X-Requested-With:XMLHttpRequest");
        headers = curl_slist_append(headers,"Referer:https://passport.csdn.net/account/loginbox?callback=logined&hidethird=1&from=http%3a%2f%2fwww.csdn.net%2f");
        headers = curl_slist_append(headers,"Connection:keep-alive");

        //设置要发往服务器的cookie
        curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "mycookie.txt");      //把服务器发过来的cookie保存到mycookie.txt

        //发送http请求头
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        char url[128];
        sprintf(url,"http://passport.csdn.net/ajax/accounthandler.ashx?t=log&u=%s&p=%s&remember=0&f=http%3A%2F%2Fwww.csdn.net%2F&rand=0.47590136872096434",strUser,strPass);
        curl_easy_setopt(curl, CURLOPT_URL,url);

        string content;
        //设置回调函数
        res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
        res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);

        //执行http请求
        res = curl_easy_perform(curl);

        string returnVal;
        Utf8ToMb((char*)content.c_str(),content.length(),returnVal);
        int pos = returnVal.find("\"status\":true");
        if ( pos >= 0){
            returnInfo.bReturn = TRUE;
        }else{
            int nStartPos = returnVal.find("error\":");
            int nEndPos   = returnVal.find("data\":",nStartPos);
            returnInfo.strErrorInfo = returnVal.substr(nStartPos+8,nEndPos-nStartPos-11);
        }

        //释放资源
        curl_easy_cleanup(curl);
        curl_slist_free_all(headers);
        headers = NULL;
    }
    curl_global_cleanup();
    return returnInfo;
}
天蓬老师天蓬老师2807일 전721

모든 응답(1)나는 대답할 것이다

  • 高洛峰

    高洛峰2017-04-17 11:15:24

    char url[128];
    sprintf(url,"http://passport.csdn.net/ajax/accounthandler.ashx?t=log&u=%s&p=%s&remember=0&f=http%3A%2F%2Fwww.csdn.net%2F&rand=0.47590136872096434",strUser,strPass);

    这个URL长度肯定不止128……

    회신하다
    0
  • 취소회신하다