lundi 31 août 2015

Memory Leak - Socket or String related?

I have a simple function that allows me to get contents of a file on a server. It works just the way I want but "Visual Leak Detector" points out there is a memory leak on line closeSocket(...).

Code below:

string executeUrl(const char *url)
{
    SOCKET sConnection;
    char szHeader[500];

    sprintf(szHeader, "GET %s HTTP/1.0\r\n"
    "Host: %s\r\n"
    "User-Agent: Agent\r\n"
    "\r\n", url, HTTPSERVER);

    sConnection = HTTPConnectToServer(HTTPSERVER);
    if (sConnection == 0)
    {
        return "";
    }
    send(sConnection, szHeader, strlen(szHeader), 0);
    char reply[1024];
    ZeroMemory(reply, 1024);
    if (recv(sConnection, reply, 1024, 0) == SOCKET_ERROR)
    {
        return "";
    }
    string returnString(reply);
    closesocket(sConnection);
    WSACleanup();
    return returnString;
}

The data leaked is the string returnString. So it's either string related or something with closesocket().

I did some reading and I can't figure it out. Apparently strings should take care of themselves and not cause memory leaks, should they?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire