Resource (memory) leaks

Memory leaks can occur if your program allocates memory and then does not free it. For example, a resource leak can occur in a memory region that no longer has references from a process.

Consequences

Resource leaks generate the following runtime errors:


Detecting the error

This error would be trapped during the following circumstances:


Enabling error detection

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


  1. In the Launch Configuration window, select the Tools tab.
  2. Expand Memory Errors and select the Perform leak check when process exits checkbox.
  3. Optional: Specify how often to check for leaks in the Perform leak check every (ms) field. The minimum depends on target speed; however, on average, it should be no less than 100 ms.

Message returned to the QNX 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 error messages for Memory Analysis.

How to address resource (memory) leaks

To address resource leaks in your program, ensure that memory is deallocated on all paths, including error paths.

Example

The following code shows an example of a memory leak:

int main(int argc, char ** argv){
  char * str = malloc(10);
  if (argc>1) {
     str = malloc(20);
     // ...
  }
  printf("Str: %s\n",str);
  free(str);
  return 0;
}
Related concepts
Illegal deallocation of memory
NULL pointer dereference
Buffer overflow
Using freed memory
Reading uninitialized memory
Functions checked for memory errors during memory analysis
Summary of error messages for Memory Analysis