If you delete[] a pointer twice then you are likely to get a program crash. It is not allowed. Your example should not be deleting url inside the function. That's rather unsafe.
Personally I recommend using const std::string& rather than const char*. When you use a std::string, you can turn it into a const char* any time you need using the .c_str() member function:
HttpResponse *HttpWrapper::urlRequest(unsignedint timeout,
const std::string& password, const std::string& url, long& httpReturnCode,
t_requestSensitivity sensitive)
{
m_sensitive = sensitive;
m_timeout = timeout;
m_timeoutHR = MS_TO_HR(timeout);
hrtime_t startHrTime = gethrtime();
*m_curlErrorMessage = 0;
if(m_curl)
{
reset();
setPassword(password);
curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str()); // convert string into const char*
curl_easy_setopt(m_curl, CURLOPT_HTTPGET, 1);
// Create the URL list for header
struct curl_slist *url_list = 0;
url_list = curl_slist_append(url_list,
"Content-Type: application/x-www-form-urlencoded");
url_list = curl_slist_append(url_list, "Expect:");
curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, url_list);
curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1);
CURLcode res = doPost(startHrTime);
curl_slist_free_all(url_list);/* free the header list */
// To get the HTTP Return code
curl_easy_getinfo(m_curl, CURLINFO_HTTP_CODE, &httpReturnCode);
// delete[] url; // Now no need to do this
if(res == 0 )
{
if(m_response->length() != 0)
{
return m_response;
}
else
{
return 0;
}
}
}
}
I would avoid using raw pointers as much as possible as they are hard to manage safely. If you must use pointers, then it is not usually a good idea to delete[] them inside a function. Better for the caller to delete[] them after the function has returned.
I always assign a pointer I've just deleted a null value, as calling delete on a null pointer has no effect. The fact that you're trying to delete it twice even if it's set to null may still indicate that you're doing something wrong, but your program won't crash.
Our system crashes ...... But i wonder its not crashing when ever the above function is called. Rather it crashes , after some 100 tread access the above function.Could any one help me to understand , why its not crashing when ever i call the above function (once).
The behavior is undefined as per the standards. So there is no point in tracing why did not crash on the first hit only. So compiler does not take any onus for this. It will depend on the internal implementation of the new/malloc/delete/free as to what and where it corrupts and when the corrupted stuff is hit again.