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_SCOPE



LEAK_SCOPE

Memory leaked leaving scope

This error is generated whenever a function allocates memory for its own use and then returns without freeing it or saving a pointer to the block in an external variable. The allocated block can never be freed.

Problem

This code calls the function gimme, which allocates a memory block that is never freed.

	1:	/*
	2:	 * File: leakscop.c
	3:	 */
	4:	#include <stdlib.h>
	5:	
	6:	void gimme()
	7:	{
	8:		char *p;
	9:		p = malloc(10);
	10:		return; bug
	11:	}
	12:
	13:	main()
	14:	{
	15:		gimme();
	16:		return (0);
	17:	}

Diagnosis (at runtime)

	[leakscop.c:10] **LEAK_SCOPE**
1.	>> 		return;
	
2.		Memory leaked leaving scope: <return>
	
3.		Lost block:	0x0003870 thru 0x00013879 (10 bytes)
				block allocated at: 
				malloc()  (interface)
			 	 gimme()  leakscop.c, 9
			  	  main()  leakscop.c, 15
		Stack trace where the error occurred:
4.				 gimme() leakscop.c, 10
			 	  main() leakscop.c, 15

  1. Source line at which the problem was detected.
  2. Description of the problem and the block that is to be lost.
  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

This problem usually results from an oversight on the part of the programmer and is cured by simply freeing a block before returning from a routine. In the current example, a call to free(p) before line 10 would cure the problem.

A particularly easy way to generate this error is to return from the middle of a routine, possibly due to an error condition arising, without freeing previously allocated data. This bug is easy to introduce when modifying existing code.

Some applications may be unable to free memory blocks and may not need to worry about their permanent loss. To suppress these error messages in this case add the option

	insure++.suppress LEAK_SCOPE

to the .psrc file.


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