Search

ParaSoft

HOME
PRODUCTS
SUPPORT
ABOUT
WHAT'S NEW
EVENTS


Insure++

Quick facts

Add-on Modules:
   -INUSE
   -TCA

Comparisons

Technical Papers

Support & Manuals

FAQs

Recent Reviews

User Testimonials

Press Releases


Insure tool to debug c++





Insure++ Reference - DELETE_MISMATCH



DELETE_MISMATCH

Inconsistent usage of delete operator

The current version of ANSI C++ distinguishes between memory allocated with new and new[]. A delete call must (according to the standard) match the new call, i.e. whether or not it has [].

Calling new[] and delete may cause the compiler to not call the destructor on each element of the array, which can lead to serious errors. Even worse, if the memory was allocated differently, memory may be corrupted. This is definitely poor practice and unlikely to work with future releases of the specific compiler.

Problem #1

The following code shows a block of memory allocated with new[] and freed with delete, without [].


	1: 	/*
	2:	 * File: delmis1.C
	3:	 */
	4:
	5:	int main() {
	6:		int *a = new int [5];
	7:		delete a; bug
	8:		return 0;
	9:	}

Diagnosis (at runtime)

1.	[delmis1.C:7] **DELETE_MISMATCH**
	>> 		delete a;
	
2.		Inconsistent usage of delete operator: a
	
3.		array deleted without []
				a, allocated at:
					main()	delmis1.C, 6
	
4.		Stack trace where the error occurred:
					main()	delmis1.C, 7
  1. Source line at which the problem was detected.
  2. Description of the problem and the operator which doesn't match.
  3. Brief description of the mismatch.
  4. Stack trace showing the function call sequence leading to the error.

Problem #2

The following code shows a block of memory allocated with new, without [], and freed with delete[]. This may cause some implementations of C++ to crash, because the compiler may look for extra bits of information about how the block was allocated. Some compilers do this to allow this type of error, extending the ANSI standard. In this case, there would be no extra bits, so the compiler would attempt to read from an invalid memory address.


	1: 	/*
	2:	 * File: delmis2.C
	3:	 */
	4:
	5:	int main() {
	6:		int *a = new int;
	7:		delete[] a; bug
	8:		return 0;
	9:	}

Diagnosis (at runtime)


1.	[delmis2.C:7] **DELETE_MISMATCH**
	>> 		delete[] a;
	
2.		Inconsistent usage of delete operator: a
	
3.		[] used to delete a non-array
				a, allocated at:
					main()	delmis2.C, 6
	
4.		Stack trace where the error occurred:
					main()	delmis2.C, 7


  1. Source line at which the problem was detected.
  2. Description of the problem and the operator which doesn't match.
  3. Brief description of the mismatch.
  4. Stack trace showing the function call sequence leading to the error.

Repair

To eliminate this error, you need to change the delete call to match the new call. In our first example, this could be accomplished by calling delete[] instead of delete, and vice versa in the second example.


< DEAD_CODE > EXPR_BAD_RANGE
Tools to debug c++ and java
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft