c++: memory not freed by free()
<p>I'm using libcurl and copying content from my http server. I need to fetch the segments and save the fetched segment in a new file.</p>

<p>The first segment is fetched correctly, but the second segment has the contents of the first segment appended to it, which it should not.</p>

<p>The memory should be freed after first segment is copied to my file but it does not happen.</p>

<p>Part of code is attached:</p>

<pre><code>//code

char *m_pBuffer = NULL;
size_t m_Size = 0;


void* Realloc(void* ptr, size_t size)
{
    if(ptr)
        return realloc(ptr, size);
    else
        return malloc(size);
};

// Callback must be declared static, otherwise it won't link...
size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb)
{
    // Calculate the real size of the incoming buffer
    size_t realsize = size * nmemb;

    // (Re)Allocate memory for the buffer
    m_pBuffer = (char*) realloc(m_pBuffer, m_Size + realsize);

    // Test if Buffer is initialized correctly &amp; copy memory
    if (m_pBuffer == NULL) {
        realsize = 0;
    }

    memcpy(&amp;(m_pBuffer[m_Size]), ptr, realsize);
    m_Size += realsize;


    // return the real size of the buffer...
    return realsize;
};

namespace ahs
{

    /* Construction / Initialisation */
    {
        return 0;
    }
    {
        curlpp::Cleanup cleaner;
        curlpp::Easy request;

        // Set the writer callback to enable cURL
        // to write result in a memory area
        curlpp::types::WriteFunctionFunctor functor(WriteMemoryCallback);
        curlpp::options::WriteFunction *test = new curlpp::options::WriteFunction(functor);
        request.setOpt(test);

        // Setting the URL to retrive.
        string abc = "http://192.168.0.34/ahs-1.0/example/ingest";
        string kk= abc + tobeused;
        request.setOpt(new curlpp::options::Url(kk));
        request.setOpt(new curlpp::options::Verbose(true));

        request.perform();

        print();
    }




    catch ( curlpp::LogicError &amp; e )
    {
        std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
    }
    catch ( curlpp::RuntimeError &amp; e )
    {
        std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
    }


    FILE *fp;
    if((fp = fopen(pp, "wb")) == NULL) {
        printf("Cannot open file.\n");
        exit(1);
    }

    if( fwrite(m_pBuffer, (long)m_Size, 1, fp) != 1) {
        printf("Write Error.\n");
        exit(1);
    }

    fclose(fp);
    free(m_pBuffer);
</code></pre>
