6278-<variable> is allocated with array new [], but deleted with scalar Delete
Consequence |
Destructors will not be called |
The calling function is allocating memoryOperator
New []
, But freeing it with the scalarOperator
Delete
.
This is an error. According to the C ++ standard and the Microsoft
Visual c ++ implementation, the result of this inconsistent operation is
Undefined. In addition, this error can cause the following problems,
Among others:
- The constructors for the individual objects in the array are invoked, but The Destructors are not invoked.
- Unexpected results are likely to occur, because the global (or class-specific)Operator
New
AndOperator
Delete
Are not compatibleOperator
New []
AndOperator
Delete []
.
Because the result of this Code is undefined, the precise
Consequences of this defect are difficult to predict. It can result in
Memory leaks in classes with Destructors that deallocate memory,
Inconsistent behavior for classes with Destructors that perform some
Semantically significant operation, or memory uptions and crashes
When operators have been overridden. In other cases, the mismatch may
Be unimportant, depending on the implementation of the compiler and its
Libraries. prefast for drivers cannot always distinguish among these
Situations.
This warning is often reported on character or wide-Character
Arrays. In this case, unless the operators are being overridden, there
May not be any significant consequences of the mismatch, but the code
Shoshould still be fixed.
In general, when you allocate memory with ArrayOperator
New []
, You shoshould free it with ArrayOperator Delete []
.
When the underlying object in the array is a primitive type, such
Int, float, Enum, or pointer, there are no Destructors that can be
Called And prefast for drivers reports
Warning 6283
.
Example
The following code example elicits this warning:
C * Pc = new C [arraysize];
Delete PC;
The following code example avoids this warning:
C * Pc = new C [arraysize];
Delete [] PC;