I'm attempting to copy a string out of a arbitrary position in a buffer. I can copy out the string but I'm left with a memory leak that I do not know how to solve. Is there a better way to do this; if not, how do I solve the memory leak.

if(_usCurrentPosition + 1 >= m_usBufferLength) // check the char
    goto ErrorOccurred;

unsigned char _ucLength = m_pcBufferData[_usCurrentPosition];
++_usCurrentPosition;

if(_usCurrentPosition + _ucLength > m_usBufferLength) // check the string
    goto ErrorOccurred;

string* str = va_arg(vl, string*);
				
char* _cTemp;
_cTemp = new char[_ucLength]; //TODO: MEMORY LEAK no delete of array				
memcpy(_cTemp, &m_pcBufferData[_usCurrentPosition], _ucLength);
(*str).assign(_cTemp, _ucLength);
//delete[] _cTemp; //causes error
_usCurrentPosition += _ucLength;

umm.. This is not native C++
use exceptions instead of goto;

use simple pointers....
You code snippet is not too clear...
or to simulate on my own end

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.