In this section:


This error is generated whenever a pointer assignment occurs which will prevent a block of dynamically allocated memory from ever being freed. Normally this happens because the pointer being changed is the only one that still points to the dynamically allocated block.


Memory leaked as a result of pointer reassignment



The following code allocates a block of memory then reassigns the pointer to the block to a static memory block. As a result, the dynamically allocated block can no longer be freed.

 * File: leakasgn.c
#include <stdlib.h>

	char *b, a[10];

	b = (char *)malloc(10);
	b = a;
	return (0);

Diagnosis at Runtime

[leakasgn.c:11] **LEAK_ASSIGN**
>>		 b = a;
Memory leaked due to pointer reassignment: b
Lost block : 0x0804b018 thru 0x0804b021 (10 bytes)
			b, allocated at leakasgn.c, 10
				malloc() (interface)
				main() leakasgn.c, 10
Stack trace where the error occurred:
				main() leakasgn.c, 11


In many cases, this problem is caused by simply forgetting to free a previously allocated block of memory when a pointer is reassigned. For example, the leak in the example code can be corrected as follows:

b = (char *)malloc(10);
b = a;

Some applications may be unable to free memory blocks and may not need to worry about their permanent loss. To suppress these error messages, suppress LEAK_ASSIGN in the Suppressions Control Panel.