In this section:

Overview

 This error is generated when any of the following occur:

  • A memory block is allocated with new and freed with free.
  • A memory block is allocated with malloc and freed with delete .
  • A memory block is allocated with one heap and freed with another heap.

CodeDescriptionEnabledReportedPlatform
ALLOC_CONFLICTMixing malloc/free with new/ delete(tick)RuntimeWindows/Unix
(badfree)

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

(tick)RuntimeWindows/Unix
(baddelet)

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

(tick)RuntimeWindows/Unix
(badhandle)

Memory is allocated by one heap and freed by a different heap

(tick)RuntimeWindows


Some compilers allow you to mix new and delete with malloc and free, but this is not good programming practice and may affect portability.

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.

/*
 * File: alloc1.cpp
 */
#include <stdlib.h>

int main() {
	char *a;

	a = new char;
	free(a);
	return 0;
}

Diagnosis at Runtime

[alloc1.cpp:10] **ALLOC_CONFLICT**
>>				free(a);
Memory allocation conflict: a

---- Associated Common Weakness Enumerations ----
CWE-762: Mismatched memory management routines
CWE-763: Release of invalid pointer or reference

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

Stack trace where the error occurred:
				main()alloc1.cpp, 10
  • Line 2: Source line at which the problem was detected.
  • Line 3: Brief description of the problem.
  • Lines 5-7: CWEs associated with this problem.
  • Line 9: Description of the conflicting allocation/deallocation.
  • Line 14: 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.

/*
 * File: alloc2.cpp
 */
 #include <stdlib.h>

 int main() {
 char *a;

 a = (char *) malloc(1);
 delete a;
 return 0;
}

Diagnosis at Runtime

[alloc2.cpp:10] **ALLOC_CONFLICT**
>> 				delete a;
Memory allocation conflict: a
 
---- Associated Common Weakness Enumerations ----
CWE-762: Mismatched memory management routines
CWE-763: Release of invalid pointer or reference

 delete operator used to deallocate memory not
	allocated by new
		block allocated at:
			malloc()(interface)
			main()alloc2.cpp, 9
Stack trace where the error occurred:
			main()alloc2.cpp, 10
  • Line 2: Source line at which the problem was detected.
  • Line 3: Brief description of the problem.
  • Lines 5-7: CWEs associated with the problem
  • Line 9: Description of the conflicting allocation/deallocation.
  • Line 14: Stack trace showing the function call sequence leading to the error.

Problem 3 (Windows)

The following code shows a typical example of allocating a block of memory with one heap handle and then freeing it with another heap handle.

/*
 * File: alloc3.cpp
 */
 #include <windows.h>

 int main() {
 HANDLE processHeap = GetProcessHeap();
 LPVOID p = HeapAlloc(processHeap, 0, 42);

 HANDLE myHeap = HeapCreate(0, 0, 0);
 LPVOID q = HeapAlloc(myHeap, 0, 42);

 HeapFree(myHeap, 0, p);
 return 0;
 }

Diagnosis at Runtime

[alloc3.cpp:13] (Thread 0) **ALLOC_CONFLICT**
>> 		HeapFree(myHeap, 0, p);
Memory allocation conflict: <argument 3>
 
---- Associated Common Weakness Enumerations ----
CWE-762: Mismatched memory management routines
CWE-763: Release of invalid pointer or reference

 heap used to deallocate memory which was allocated in a different heap
		p, allocated at alloc3.cpp, 8
			HeapAlloc() (interface)
				main() alloc3.cpp, 8

Stack trace where the error occurred:
			HeapFree() (interface)
				main() alloc3.cpp, 13 
  • Line 2: Source line at which the problem was detected.
  • Line 3: Brief description of the problem.
  • Lines 5-7: CWEs associated with the problem.
  • Line 9: Description of the conflicting allocation/deallocation.
  • Line 14: Stack trace showing the function call sequence leading to the error.

Problem 4 (Windows)

The following code shows an example of a subtle memory allocation conflict. The main executable frees a memory block allocated by a dynamic-link library (DLL). The library is linked dynamically with the C runtime library, but the executable is linked statically instead. Thus, the executable and the library use different copies of the C runtime library and different C library heaps.

DLL
/*
 * File: alloc4dll.cpp
 *
 * Build with "insure /Zi /MD /LD alloc4dll.cpp".
 */

#include <stdlib.h>

__declspec(dllexport) void *foo()
{
	void *p = malloc(10);
	return p;
}
Main executable
/*
 * File: alloc4exe.cpp
 *
 * Build with "insure /Zi alloc4exe.cpp alloc4dll.lib".
 */

#include <stdlib.h>

void *foo();

int main()
{
	void *p = foo();
 	free(p);
	return 0;
}

Diagnosis at Runtime

[alloc4exe.cpp:14] (Thread 0) **ALLOC_CONFLICT**
>> 		free(p);
  Memory allocation conflict: <argument 1> 

---- Associated Common Weakness Enumerations ----
CWE-762: Mismatched memory management routines
CWE-763: Release of invalid pointer or reference

heap used to deallocate memory which was allocated in a different heap
		p, allocated at alloc4dll.cpp, 11
			malloc() (interface)
			foo() alloc4dll.cpp, 11
			main() alloc4exe.cpp, 13

Stack trace where the error occurred:
			free() (interface)
			main() alloc4exe.cpp, 14
  • Line 2: Source line at which the problem was detected.
  • Line 3: Brief description of the problem.
  • Lines 5-7: CWEs associated with the problem.
  • Line 9: Description of the conflicting allocation/deallocation.
  • Line 15: 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.

References

The table below shows Common Weakness Enumerations associated with this error.

CWEDescription
CWE-762Mismatched Memory Management Routines
CWE-763Release of Invalid Pointer or Reference
  • No labels