Dynamic allocated array is not freed

Posted by Stefano on Stack Overflow See other posts from Stack Overflow or by Stefano
Published on 2011-11-28T17:44:33Z Indexed on 2011/11/28 17:55 UTC
Read the original article Hit count: 183

Filed under:
|
|
|

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];
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays