---
title: Strict memory safety (StrictMemorySafety)
framework: swift-compiler
role: article
role_heading: Article
path: swift-compiler/documentation/diagnostics/strict-memory-safety
---

# Strict memory safety (StrictMemorySafety)

Warnings that identify the use of language constructs and library APIs that can undermine memory safety, disabled by default.

## Overview

Overview Strict memory safety can be enabled with the -strict-memory-safety compiler flag. Examples of memory-unsafe code in Swift include: Use of a function or type annotated with @unsafe: func getPointee<T>(_ pointer: UnsafeMutablePointer<Int>, as type: T.Type) -> T {   // reference to unsafe global function 'unsafeBitCast'   return unsafeBitCast(pointer.pointee, to: type) } Use of a function involving an @unsafe type: func evilMalloc(size: Int) -> Int {   // use of global function 'malloc' involves unsafe type 'UnsafeMutableRawPointer'   return Int(bitPattern: malloc(size)) } Use of an entity that makes use of an unsafe language feature: // use of an unowned(unsafe) variable is not memory-safe unowned(unsafe) var parentNode: TreeNode<T> Definition of a type whose storage contains unsafe types: // struct MyTemporaryBuffer has storage involving unsafe types struct MyTemporaryBuffer<T> {   private var storage: UnsafeBufferPointer<T> } Using an @unsafe operation to satisfy a safe protocol requirement: // conformance of 'MyType' to protocol 'CustomStringConvertible' involves unsafe code struct MyType: CustomStringConvertible {   @unsafe var description: String {     "I am unsafe!"   } } The warnings produced by strict memory safety can be suppressed by acknowledging the unsafe behaior with one of three constructs: unsafe expressions to acknowledges that there exists memory-unsafe code within the given expression, similar to the way try and await work for throwing and asynchronous functions: func evilMalloc(size: Int) -> Int {   return unsafe Int(bitPattern: malloc(size)) } @unsafe attribute, which acknowledges that a type or conformance is also unsafe: struct MyType: @unsafe CustomStringConvertible {   @unsafe var description: String {     "I am unsafe!"   } } @safe attribute to indicate that an entity encapsulates the unsafe behavior to provide a safe interface: @safe struct MyTemporaryBuffer<T> {   private var storage: UnsafeBufferPointer<T> }

## See Also

- [@dynamicCallable implementation requirements (DynamicCallable)](swift-compiler/documentation/diagnostics/dynamic-callable-requirements.md)
- [Add @preconcurrency import (AddPreconcurrencyImport)](swift-compiler/documentation/diagnostics/add-preconcurrency-import.md)
- [Always enabled availability domains (AlwaysAvailableDomain)](swift-compiler/documentation/diagnostics/always-available-domain.md)
- [Argument matching for trailing closures (TrailingClosureMatching)](swift-compiler/documentation/diagnostics/trailing-closure-matching.md)
- [Calling a mutating async actor-isolated method (ActorIsolatedMutatingAsync)](swift-compiler/documentation/diagnostics/actor-isolated-mutating-async.md)
- [Calling an actor-isolated method from a synchronous nonisolated context (ActorIsolatedCall)](swift-compiler/documentation/diagnostics/actor-isolated-call.md)
- [Captures in a `@Sendable` closure (SendableClosureCaptures)](swift-compiler/documentation/diagnostics/sendable-closure-captures.md)
- [Compilation caching (CompilationCaching)](swift-compiler/documentation/diagnostics/compilation-caching.md)
- [Conforming to `StringInterpolationProtocol` (StringInterpolationConformance)](swift-compiler/documentation/diagnostics/string-interpolation-conformance.md)
- [Conversion from `@isolated(any)` function type to synchronous function type (ConversionFromIsolatedAnyToSynchronous)](swift-compiler/documentation/diagnostics/conversion-from-isolated-any-to-synchronous.md)
- [Cross-isolation data race (RegionIsolationCrossIsolationDataRace)](swift-compiler/documentation/diagnostics/region-isolation-cross-isolation-data-race.md)
- [Deprecated declaration warnings (DeprecatedDeclaration)](swift-compiler/documentation/diagnostics/deprecated-declaration.md)
- [Deprecated implementation-only imports (ImplementationOnlyDeprecated)](swift-compiler/documentation/diagnostics/implementation-only-deprecated.md)
- [Dynamic exclusivity (DynamicExclusivity)](swift-compiler/documentation/diagnostics/dynamic-exclusivity.md)
- [Embedded Swift language restrictions (EmbeddedRestrictions)](swift-compiler/documentation/diagnostics/embedded-restrictions.md)
