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



LEAK_ASSIGN

Memory leaked due to pointer reassignment

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.

Problem

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

	1:	/*
	2:	 * File: leakasgn.c
	3:	 */
	4:	#include <stdlib.h>
	5:	
	6:	main()
	7:	{
	8:		char *b, a[10];
	9:
	10:		b = (char *)malloc(10);
	11:		b = a; bug
	12:		return (0);
	13:	}

Diagnosis (at runtime)

	[leakasgn.c:11] **LEAK_ASSIGN**
1.	>> 		b = a;
	
2.		Memory leaked due to pointer reassignment: <return>
	
3.		Lost block:	0x000173e8 thru 0x000173f1 (10 bytes)
				block allocated at: 
				malloc() (interface)
			  	  main() leakasgn.c, 10

		Stack trace where the error occurred:
4.				  main() leakasgn.c, 11
  1. Source line at which the problem was detected.
  2. Description of the problem and the expression that is in error.
  3. Description of the block of memory that is about to be lost, including its size and the line number at which it was allocated.
  4. Stack trace showing the function call sequence leading to the error.

Repair

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

	10:  b = (char *)malloc(10);
	11:  free(b);
	12:  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, add the option

	insure++.suppress LEAK_ASSIGN

to the .psrc file.


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