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



FREE_NULL

Freeing NULL pointer

This error is generated whenever an attempt is made to de-allocate memory using the NULL pointer.

Problem

This code attempts to free the pointer a, which has never been assigned. Since this is a global variable, it is initialized to zero by default. This results in the code attempting to free a NULL pointer.


	1:	/*
	2:	 * File: freenull.c
	3:	 */
	4:	char *a;
	5:
	6:	main()
	7:	{
	8:		free(a); bug
	9:		return (0);
	10:	}

Diagnosis


	[freenull.c:8] **FREE_NULL**
1.	>> 		free(a);
	
2.		Freeing null pointer: a

		Stack trace where the error occurred:
3.			main() freenull.c, 8
	
4.		**Memory corrupted. Program may crash!!**


  1. Source line at which the problem was detected.
  2. Description of the problem and the expression that is in error.
  3. Stack trace showing the function call sequence leading to the error.
  4. Informational message indicating that a serious error has occurred which may cause the program to crash.

Repair

Some systems allow this operation, but this is not portable programming practice and is not recommended.

A potential cause of this error is the one shown in the example - a pointer that never got explicitly initialized before being used. The given example can be corrected by adding an allocation as follows

	/*
	 * File: freenull.c (modified)
	 */
	#include <stdlib.h>
	char *a;
	
	main()
	{
	     a = (char *)malloc(100);
	     free(a);
	     return(0);
	}

A second fairly common possibility is that a block of dynamically allocated memory associated with the pointer has already been freed, and its pointer reset to NULL. In this case, the error could mean that a second attempt is being made to free the same memory block.

A final common problem is caused when one of the dynamic memory allocation routines, malloc, calloc, or realloc, fails and returns a NULL pointer. This can happen either because your program passes bad arguments, or simply because it asks for too much memory. A simple way of finding this problem with Insure++ is to enable the RETURN_FAILURE error code via your .psrc file and run the program again. It will then issue diagnostic messages every time a system call fails, including the memory allocation routines.

If your application needs to free NULL pointers, you can suppress these error messages by adding the option

	insure++.suppress FREE_NULL

to the .psrc file.


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