Where should I free the object?
<pre><code>void ServerManager::addWork(SocketClient *clientSocket, vector&lt;char&gt; data){
    Work *work = new Work(clientSocket, data);
    m_workHandler-&gt;addWork(work);
}
</code></pre>

<p>Here I created Work instance and hand the work to the WorkHandler. </p>

<pre><code>m_workHandler-&gt;addWork(work);
</code></pre>

<p>This code is inserting the work into the queue. </p>

<p>This is one part of my server and if I run this code for long time then the server crashes with error saying like this</p>

<pre><code>what() std::bad_alloc
</code></pre>

<p>I looked online and it is an error message that show there isn't memory space available. So I realized I didn't free the work object after I am done with using it. </p>

<p>So I was wondering where I should free the work object. </p>

<pre><code>void ServerManager::addWork(SocketClient *clientSocket, vector&lt;char&gt; data){
    Work *work = new Work(clientSocket, data);
    m_workHandler-&gt;addWork(work);
    delete work;
}
</code></pre>

<p>Can I do this? If I pass the work instance then it increases the reference count? </p>

<pre><code>void WorkHandler::workLoop(){
    printf("WorkHandler::workLoop, called\n");

    while(m_workHandlerRun){
        Work *work = getWork();
        char *pdata = work-&gt;getVPointer();

        unsigned char type = pdata[0];

        printf("WorkHandler::workLoop, type %d\n", type);

        Packet *packet = m_packetFactory-&gt;createInstance(static_cast&lt;PACKET_TYPES&gt;(type));
        packet-&gt;fromByte(pdata);

        delete work;
    }
}
</code></pre>

<p>This is the code I handle the work....Should I delete work instance here or both place? Thanks in advance...</p>
