Thread-specific memory leak
Dynamically allocated thread-specific memory not freed before end of thread
Description
This checker is deactivated in a default Polyspace® as You Code™ analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace as You Code).
This defect occurs when you do not free thread-specific dynamically allocated memory before the end of a thread.
To create thread-specific storage, you generally do these steps:
You create a key for thread-specific storage.
You create the threads.
In each thread, you allocate storage dynamically and then associate the key with this storage.
After the association, you can read the stored data later using the key.
Before the end of the thread, you free the thread-specific memory using the key.
The checker flags execution paths in the thread where the last step is missing.
The checker works on these families of functions:
tss_getandtss_set(C11)pthread_getspecificandpthread_setspecific(POSIX)
Risk
The data stored in the memory is available to other processes even after the threads end (memory leak). Besides security vulnerabilities, memory leaks can shrink the amount of available memory and reduce performance.
Fix
Free dynamically allocated memory before the end of a thread.
You can explicitly free dynamically allocated memory with functions such as
free.
Alternatively, when you create a key, you can associate a destructor function with the key. The destructor function is called with the key value as argument at the end of a thread. In the body of the destructor function, you can free any memory associated with the key. If you use this method, Bug Finder still flags a defect. Ignore this defect with appropriate comments. See:
Address Polyspace Results Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
Examples
Result Information
| Group: Concurrency |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax:
THREAD_MEM_LEAK
|
| Impact: Medium |
PQL Name:
std.defects.THREAD_MEM_LEAK
|
Version History
Introduced in R2018b
See Also
Topics
- Interpret Polyspace Bug Finder Results in Polyspace Platform User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Polyspace Results Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)