Illegal deallocation of memory

The illegal deallocation of memory occurs when a free operation is performed on a pointer that doesn't point to an appropriate heap memory segment. This type of error can occur when you attempt to do any of the following activities:

Consequences

The illegal deallocation of memory can generate the following runtime errors:

Detecting the error

In the IDE, the Memory Analysis tool detects this error (if error detection is enabled), and it traps the illegal deallocation error when any of the following functions are called:

Note: For instructions about enabling error detection in the IDE, see Enabling memory leak detection.

Enabling error detection for the illegal deallocation of memory

To enable error detection for the illegal deallocation of memory:

  1. In the Launch Configuration window, select the Tools tab.
  2. Expand Memory Errors and select the Enable error detection checkbox.
  3. Select the Enable check on realloc()/free() argument checkbox.
  4. Click OK.

Message returned to the IDE

In the IDE, you can expect the message for this type of memory error to include the following types of information and detail:

For a list of error messages returned by the Memory Analysis tool, see Summary of Memory Analysis errors.

How to address the illegal deallocation of memory

To help address this memory problem, try the following:

Example

The following code shows an example of the illegal deallocation of memory:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
                     
int main(int argc, char ** argv){ 
char * str = ""; 
if (argc>1) { 
str = malloc(10); 
// ... 
} 
printf("Str: %s\n",str); 
free(str); 
return 0; 
}