2015-07-10 35 views
0

當我執行curl_easy_perform我那麼做了curl_easy_cleanup像這樣:curl_easy_cleanup犯規明確捲曲指針

CURL* pEasy = nullptr; 
pEasy = curl_easy_init(); 
if (pEasy != nullptr) 
{ 
    curl_easy_setopt(pEasy, CURLOPT_USERNAME, user.c_str()); 
    curl_easy_setopt(pEasy, CURLOPT_PASSWORD, pass.c_str()); 
    curl_easy_setopt(pEasy, CURLOPT_URL, urlToConnectTo.c_str()); 
    curl_easy_setopt(pEasy, CURLOPT_WRITEFUNCTION, OnReceiveHttpResponse); 

    curl_easy_perform(pEasy); 

    curl_easy_cleanup(pEasy); 
} 

我在調試注意到pEasy指針的地址和它說0x2af0ad18但之後,我它的curl_easy_cleanup其仍然0x2af0ad18。

之後我必須將它設置爲nullptr嗎? 我會期待curl_easy_cleanup爲我重置這個嗎?

+0

_「我是否必須將它設置爲nullptr?」_可能。分配的內存從'pEasy'中的地址中釋放並不會自動將其設置爲'nullptr'。 –

回答

2

http://curl.haxx.se/libcurl/c/curl_easy_cleanup.html

該功能需要一個指針,你會怎麼指望它改變你的本地範圍的東西的價值?

當你調用free(x),它不設置設置xnull它只是釋放內存,就是curl_easy_cleanup正在做同樣的事情。無論你想將pEasy設置爲nullptr還是不取決於你,可能都是很好的做法,但根據代碼的佈局以及pEasy是否即將超出範圍而不必要。