Search code examples
c++memorymaya

Unable to free memory for a given Maya type


In C++, I can reserve some memory and then delete this memory, like:

float *myFloat;
myFloat = new float[10];
delete myFloat;  --> Works fine

However if the type is not a float * but a MTransformationMatrix * (Maya type), then I am unable to delete:

MTransformationMatrix *myTransformationMatrixes;
myTransformationMatrixes = new MTransformationMatrix[10];
delete myTransformationMatrixes;   --> Crash

Is there anything I need to do for special types in order to free memory ?


Solution

  • Both of these allocated objects are arrays, you should use the delete[] syntax to delete them:

    float *myFloat = new float[10];
    delete[] myFloat;
    
    MTransformationMatrix *myTransformationMatrixes;
    myTransformationMatrixes = new MTransformationMatrix[10];
    delete[] myTransformationMatrixes;
    

    Both of your examples invoke undefined behavior, you are just lucky the first one did not cause visible harm.