Dynamic allocated array is not freed
- by Stefano
I'm using the code above to dynamically allocate an array, do some work inside the function, return an element of the array and free the memory outside of the function. But when I try to deallocate the array it doesn't free the memory and I have a memory leak. The debugger pointed to the myArray variable shows me the error CXX0030. Why?
struct MYSTRUCT
{
char *myvariable1;
int myvariable2;
char *myvariable2;
....
};
void MyClass::MyFunction1()
{
MYSTRUCT *myArray= NULL;
MYSTRUCT *myElement = this->MyFunction2(myArray);
...
delete [] myArray;
}
MYSTRUCT* MyClass::MyFunction2(MYSTRUCT *array)
{
array = (MYSTRUCT*)operator new(bytesLength);
...
return array[X];
}