---
title: Thread leaks
framework: xcode
role: article
role_heading: Article
path: xcode/thread-leaks
---

# Thread leaks

Detects when you don’t close threads after use.

## Overview

Overview Use this check to detect threads you create using the pthread_create(_:_:_:_:) function without a corresponding call to the pthread_join(_:_:) function. Leaked threads can result in decreased performance and may lead to crashes. Available in Xcode 8 and later. Leaked thread in C In the following example, the code creates a thread variable, but doesn’t close it after use: void *run(){     pthread_exit(0); } pthread_t thread; pthread_create(&thread, NULL, run, NULL); // Error: thread leak sleep(1); Solution Add a call to the pthread_join(_:_:) function. void *run(){     pthread_exit(0); } pthread_t thread; pthread_create(&thread, NULL, run, NULL); sleep(1); pthread_join(thread, NULL); // Correct Alternatively, you can create a detached thread by passing the PTHREAD_CREATE_DETACHED attribute to pthread_create(_:_:_:_:), or calling pthread_detach(_:) on the thread after creation.

## See Also

### Thread Sanitizer

- [Data races](xcode/data-races.md)
- [Swift access races](xcode/swift-access-races.md)
- [Races on collections and other APIs](xcode/races-on-collections-and-other-apis.md)
- [Uninitialized mutexes](xcode/uninitialized-mutexes.md)
