PREfast Warning 278 (Windows CE 5.0)
278 - Allocation/free mismatch.
Additional Information: <variable> is allocated with array new [], but deleted with scalar delete (see allocation at <location>).
Consequence: Destructors will not be called.
This warning appears only in C++ code and indicates that the calling function has inconsistently allocated memory with the array new operator but freed it with the scalar delete operator.
This is undefined behavior according to the C++ standard and the Microsoft Visual C++ implementation.
This can cause problems for these reasons:
- The constructors for the individual objects in the array are invoked, but the destructors are not invoked.
- If global (or class-specific) operator new and operator delete are not compatible with operator new and operator delete, unexpected results can occur.
The results of this defect are difficult to predict. It can result in
- Leaks (for classes with destructors that perform memory deallocation)
- Inconsistent behavior (for classes with destructors that perform some semantically significant operation)
- Memory corruptions and crashes (if operators have been overridden)
In other cases, the mismatch might be unimportant, depending on the implementation of the compiler and its libraries. PREfast cannot always distinguish between these situations.
If memory is allocated with arraynew, it should be typically be freed with arraydelete.
If the underlying object in the array is a primitive type such as int, float, enum, or pointer, there are no destructors to be called. In this case, warning 283 is reported instead.
This message is often reported on character or wide-character arrays. In this case, unless operators are being overridden, there might not be significant consequences of the mismatch. It might be more appropriate to treat the warning as a maintenance issue instead of as an active defect.
Example
Defective Source
C *pC = new C[arraySize];
delete pC;
Corrected Source
C *pC = new C[arraySize];
delete[] pC;
Send Feedback on this topic to the authors