Re: Cannot use DEBUG_NEW to trace this leak.
"Simon" <email@example.com> ha scritto nel messaggio
I have narrowed down the code as follow where the error could be.
I would rather not change the code as much as possible.
// in the constructor
m_pData = new std::string[ someKnownSize + 1]
What are you doing here?
Are you allocating a raw vector (new) of std::string's?
The above code seems erroneous to me.
If you want to allocate a vector (list) of strings, I would suggest
typedef std::vector< std::string > StringList;
StringList myStrings( howManyStrings );
Or do you want to allocate a *single* string?
In this case, just a std::string instantiation is fine.
memset( m_pData, 0, (n+1)*sizeof(std::string) );
Please use memset() only for POD (plain old data).
And sizeof(std::string) is not very meaningful in this context...
// The destructor
if( NULL != m_pData )
delete  ( m_pData );
m_pData = NULL;
m_nSize = 0;
If you use std::vector< std::string >, you don't need to do anything in the
destructor; std::vector and std::string destructors properly take care of
their own resource cleanup.
// in a function
void add( std::string &s, int len )
m_pData[m_nSize++] = std::string(s, len ) ;
if I remove the lines m_pData[m_nSize++] = std::string(s, len ) ; then
I have no leak.
I would use std::vector< std::string > to store the list of strings.
It's very easy:
std::vector< std::string > myStrings;
myStrings.push_back( std::string( "Bob" ) );
myStrings.push_back( std::string( "Jeff" ) );
And if you want to access the i-th string in the list, you can use:
myStrings[i] // no bounds checking on index 'i'
myStrings.at(i) // more safe, does bounds checking on 'i'