Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding Reason For Memory Leaking With Char*
    primarykey
    data
    text
    <p>I been working in a project that handles some char* pointers, and it's a requisite of the class to use char* instead of std::string, so...</p> <p>I have this structure definition and this queue: </p> <pre><code>typedef struct packetQueue { char* buf; int length; packetQueue() { buf = new char[]; length = 0; } } PACKET; </code></pre> <p>concurrency::concurrent_queue IP_in_queue;</p> <p>I have this buffer:</p> <pre><code>char sendBuf[MSG_SIZE + sizeof (IP_PACKET_HEADER_T) + 1]; // String to be send </code></pre> <p>and a structure for my new buffer:</p> <pre><code>PACKET ipQueue; </code></pre> <p>then I fill my buffer with this:</p> <pre><code>// Concatenates the header with sended message memcpy(sendBuf, (void*)&amp;sendHeader, sizeof(sendHeader)); memcpy(&amp;sendBuf[sizeof(sendHeader)], readMessage, sendHeader.length); ipQueue.buf = sendBuf; ipQueue.length = packetSize; </code></pre> <p>And then I push my packet to my queue IP_in_queue.push(ipQueue); // Push the buffer in the IP_in_queue</p> <p>This is my loop just in case:</p> <pre><code>while ( 1 ) { // Get the user input cout &lt;&lt; "&gt; "; cin.getline (buf, BUFLEN); IP_PACKET_HEADER_T sendHeader; // Store the header to be send PACKET ipQueue; char* fakeIPAddressDst, *readMessage; delay = atoi(strtok (buf," ")); // Takes the first delay value fakeIPAddressDst = strtok (NULL, " "); // Stores the IP Address readMessage = strtok (NULL, " "); // Stores the sended message Sleep(delay); // Sleep the miliseconds defined // Fills the header with the data neccesary data sendHeader.DIP = inet_addr(fakeIPAddressDst); sendHeader.SIP = inet_addr(initAddress.fakeIpAddress); sendHeader.length = getStringLength(readMessage) + 1; packetSize = sizeof( sendHeader ) + sendHeader.length; // Defines the size of the packet to be send // Concatenates the header with sended message memcpy(sendBuf, (void*)&amp;sendHeader, sizeof(sendHeader)); memcpy(&amp;sendBuf[sizeof(sendHeader)], readMessage, sendHeader.length); ipQueue.buf = sendBuf; ipQueue.length = packetSize; numbytes = packetSize; // The number of bytes of sended buffer char sendedString[BUFLEN + 1]; // Variable for stores the data IP_PACKET_HEADER_T readHeader; // To store the header for showing the information // Print out the content of the packet // Copy from buf to the header memcpy( (void*)&amp;readHeader, ipQueue.buf, sizeof( IP_PACKET_HEADER_T)); // Copy message part memcpy( sendedString, &amp;ipQueue.buf[sizeof(IP_PACKET_HEADER_T)], numbytes - sizeof(IP_PACKET_HEADER_T)); // Append \0 to the end sendedString[numbytes - sizeof(IP_PACKET_HEADER_T)] = '\0'; // Save the IP information of the packet in a struct for print on the screen struct in_addr fakeAddrHost; fakeAddrHost.s_addr = readHeader.SIP; // Print the neccesary data cout &lt;&lt; "[IN] DST: " &lt;&lt; fakeIPAddressDst &lt;&lt; endl; // Fake IP address of the destination cout &lt;&lt; "[IN] SRC: " &lt;&lt; inet_ntoa(fakeAddrHost) &lt;&lt; endl; // Fake IP address of the host cout &lt;&lt; "[IN] MSG: " &lt;&lt; sendedString &lt;&lt; endl ; // Message to send IP_in_queue.push(ipQueue); // Push the buffer in the IP_in_queue } </code></pre> <p>I know there is a memory leak in this procedure but I'm not sure. When I push my packet, the buf pointer keeps pointing to my sendBuf, am I right? Because the assignment does that, but if I delete my pointer in the ipQueue after I push the program crashes. I have to say, after I push that struct into the queue, another thread try to pop that one, and obviously if I delete my ipQueue pointer I'll lost my buffer, so how can I avoid this memory leak?</p> <p>Thanks</p> <p>EDIT:</p> <p>The memory leak using the definition of buf = nullptr</p> <pre><code>---------- Block 1 at 0x0068BB30: 264 bytes ---------- Call Stack: d:\program files (x86)\microsoft visual studio 11.0\vc\include\concurrent_queue.h (402): Host.exe!Concurrency::concurrent_queue&lt;packetQueue,std::allocator&lt;packetQueue&gt; &gt;::_Allocate_page + 0xF bytes f:\dd\vctools\crt_bld\self_x86\crt\src\concurrent_queue.cpp (113): MSVCP110D.dll!Concurrency::details::_Micro_queue::_Push + 0xD bytes f:\dd\vctools\crt_bld\self_x86\crt\src\concurrent_queue.cpp (232): MSVCP110D.dll!Concurrency::details::_Concurrent_queue_base_v4::_Internal_push d:\program files (x86)\microsoft visual studio 11.0\vc\include\concurrent_queue.h (566): Host.exe!Concurrency::concurrent_queue&lt;packetQueue,std::allocator&lt;packetQueue&gt; &gt;::push + 0xF bytes d:\users\silex rpr\documents\visual studio 2012\projects\project2\project2\host.cpp (802): Host.exe!main f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (536): Host.exe!__tmainCRTStartup + 0x19 bytes f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): Host.exe!mainCRTStartup 0x7662339A (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes 0x77179EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes 0x77179EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload