Debug_VLD in VS2010 reveals some memory leaks that come from class member creation / initialization / deletion.
my_member
is a data member with type double*. In constructor, I have
my_member = NULL ;
Then in some method, i need to allocate memory for my_member
. I cannot do so in constructor,
since i dont know size of array yet, and/or size may different for different calls of the method. What i do in this method is checking if member is NULL. if so, i allocate space for it, if not, i can operate on array (changing value for its element with accesor []). It looks like
void MyClass::my_method()
{
if( my_member == NULL )
my_member = new double[n_dim] ;
for(int k = 0 ; k < n_dim ; k++ )
my_member[k] = k ;
}
and memory leak occurs at line my_member = new double[n_dim] ;
.
In destructor, i have
delete[] my_member ;
what is wrong? how to do allocation properly ?
thanks!