---
title: Use of stack memory after function return
framework: xcode
role: article
role_heading: Article
path: xcode/use-of-stack-memory-after-function-return
---

# Use of stack memory after function return

Detects when you access stack variable memory after its declaring function returns.

## Overview

Overview Use this check to detect access to a stack variable that a function declares after that function returns. Attempting to access stack memory in this manner can lead to crashes, or result in unpredictable behavior. Available in Xcode 9 and later. note: This check is in a disabled state by default. You can enable it under the Address Sanitizer option in the Edit Scheme dialogue. Use of stack memory after return in C In the following example, the integer_pointer_returning_function function returns a pointer to a stack variable, and there’s an attempt to access the memory of the returned pointer: int *integer_pointer_returning_function() {     int value = 42;     return &value; }

int *integer_pointer = integer_returning_function(); *integer_pointer = 43; // Error: invalid access of returned stack memory Solution Use pointer arguments to allow a function to return values by reference.

## See Also

### Address Sanitizer

- [Use of deallocated memory](xcode/use-of-deallocated-memory.md)
- [Deallocation of deallocated memory](xcode/deallocation-of-deallocated-memory.md)
- [Deallocation of nonallocated memory](xcode/deallocation-of-nonallocated-memory.md)
- [Use of out-of-scope stack memory](xcode/use-of-out-of-scope-stack-memory.md)
- [Overflow and underflow of buffers](xcode/overflow-and-underflow-of-buffers.md)
- [Overflow of C++ containers](xcode/overflow-of-c-containers.md)
