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



PARM_DANGLING

Array parameter is dangling pointer

This error is generated whenever a parameter declared as an array is actually passed a pointer to a block of memory that has been freed.

Problem

The following code frees its memory block before passing it to foo.

	1:	/*
	2:	 * File: parmdngl.c
	3:	 */
	4:	#include <stdlib.h>
	5:
	6:	char foo(a)
	7:		char a[10];
	8:	{ bug
	9:		return a[0]; 	
	10:	}
	11:
	12:	main()
	13:	{	
	14:		char *a;
	15:		a = (char *)malloc(10);
	16:		free(a);
	17:		foo(a);
	18:		return (0);
	19:	}

Diagnosis (at runtime)

	[parmdngl.c:8] **PARM_DANGLING**
1.	>> 	{
	
2.		Array parameter is dangling pointer: a
	
3.		Pointer		: 0x0001adb0
4.		In block	: 0x0001adb0 thru 0x0001adb9 (10 bytes)
			     block allocated at: 
				malloc() (interface)
			  	  main() parmdngl.c, 15
	
			     stack trace where memory was freed: 
5.			  	 main () parmdngl.c, 16

		Stack trace where the error occurred:
				   foo() parmdngl.c, 8
6.				  main() freedngl.c,17
  1. Source line at which the problem was detected.
  2. Description of the problem and the parameter that is in error.
  3. Value of the pointer that was passed and has been deallocated.
  4. Information about the block of memory addressed by this pointer, including information about where this block was allocated.
  5. Indication of the line at which this block was freed.
  6. Stack trace showing the function call sequence leading to the error.

Repair

This error is normally caused by freeing a piece of memory too soon.

A good strategy is to examine the line of code indicated by the diagnostic message which shows where the memory block was freed and check that it should indeed have been de-allocated.

A second check is to verify that the correct parameter was passed to the subroutine.

A third strategy which is sometimes useful is to NULL pointers that have been freed and then check in the called subroutine for this case. Code similar to the following is often useful

	#include <stdlib.h>

	char foo(a)
		char *a;
	{
		if(a) return a[0];
		return '!';
	}

	main()
	{  
		char *a;
		a = (char *)malloc(10);
		free(a);
		a = NULL;
		foo(a);
       		return (0);
	}

The combination of resetting the pointer to NULL after freeing it and the check in the called subroutine prevents misuse of dangling pointers.


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