无法释放给定 Maya 类型的内存

Unable to free memory for a given Maya type

在C++中,我可以预留一些内存然后删除这块内存,比如:

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

但是如果类型不是float *而是MTransformationMatrix *(Maya类型),那么我无法删除:

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

我需要为特殊类型做些什么来释放内存吗?

这两个分配的对象都是数组,你应该使用delete[]语法来删除它们:

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

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

你的两个例子都调用了未定义的行为,你很幸运,第一个没有造成明显的伤害。

您应该使用 delete[] 运算符删除数组。 当只分配单个对象而不是数组时使用 delete

当你使用错误的时候,会导致未定义的行为。