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 - ALLOC_CONFLICT



ALLOC_CONFLICT

C++

Memory allocation conflict

This error is generated when a memory block is allocated with new(malloc) and freed with free(delete).

Insure++ distinguishes between the two possibilities as follows:

badfree
Memory was allocated with new or new[] and an attempt was
made to free it with free.
baddelete
Memory was allocated with malloc and an attempt was
made to free it with delete or delete[] .

Some compilers do allow this, but it is not good programming practice and could be a portability problem.


Problem #1

The following code shows a typical example of allocating a block of memory with new and then freeing it with free, instead of delete.


	1:	/*
	2:	 * File: alloc1.C
	3:	 */
	4:	#include <stdlib.h>
	5:	
	6:	int main() {
	7:		char *a;
	8:
	9:		a = new char;
	10:		free(a); bug
	11:		return 0;
	12:	}


Diagnosis (at runtime)


1.	[alloc1.C:10] **ALLOC_CONFLICT**
	>> 		free(a);

2.		Memory allocation conflict: a

3.		free() used to deallocate memory which was allocated
			using new
					a, allocated at:
						main()	alloc1.C, 9

4.		Stack trace where the error occurred:
						main()	alloc1.C, 10

  1. Source line at which the problem was detected.
  2. Brief description of the problem.
  3. Description of the conflicting allocation/deallocation.
  4. Stack trace showing the function call sequence leading to the error.

Problem #2

The following code shows another typical example of this type of error, allocating a block of memory with malloc and then freeing it with delete.


	1:	/*
	2:	 * File: alloc2.C
	3:	 */
	4:	#include <stdlib.h>
	5:	
	6:	int main() {
	7:		char *a;
	8:
	9:		a = (char *) malloc(1);
	10:		delete a; bug
	11:		return 0;
	12:	}

Diagnosis (at runtime)


1.	[alloc2.C:10] **ALLOC_CONFLICT**
	>> 		delete a;

2.		Memory allocation conflict: a

3.		delete operator used to deallocate memory not
			allocated by new
				block allocated at:
				      malloc()	(interface)
					main()	alloc2.C, 9

4.		Stack trace where the error occurred:
					main()	alloc2.C, 10

  1. Source line at which the problem was detected.
  2. Brief description of the problem.
  3. Description of the conflicting allocation/deallocation.
  4. Stack trace showing the function call sequence leading to the error.

Repair

This type of error can be corrected by making sure that all your memory allocations match up.


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