---
title: Foreign reference types (ForeignReferenceType)
framework: swift-compiler
role: article
role_heading: Article
path: swift-compiler/documentation/diagnostics/foreign-reference-type
---

# Foreign reference types (ForeignReferenceType)

Warnings related to foreign reference types imported from C and C++.

## Overview

Overview Swift imports C and C++ types annotated with SWIFT_SHARED_REFERENCE as foreign reference types (FRTs). Their memory is managed in a similar manner to native Swift classes: the compiler automatically inserts calls to retain and release operations that maintain an FRT’s reference count. When a Swift program calls a foreign function returning an FRT, the compiler relies on ownership convention annotations to determine whether it needs to insert a retain call on behalf of the Swift program: SWIFT_RETURNS_RETAINED means the FRT is returned as an owned object (+1 reference count) SWIFT_RETURNS_UNRETAINED means the FRT is returned as an unowned object (+0 reference count) Without these annotations, the compiler can generate code with a superfluous or missing retain call that leads to memory leaks and errors, so it emits the following warning: warning: cannot infer ownership of foreign reference value returned by 'returnsFRT()' Fixing the warning To address these warnings, you should add the appropriate annotation to your C or C++ function declaration: // Returns a retained (+1) object SWIFT_RETURNS_RETAINED MyFRT* createObject();

// Returns an unretained (+0) object SWIFT_RETURNS_UNRETAINED MyFRT* getExistingObject(); Objective-C and Objective-C++ methods can be similarly annotated: // Returns a retained (+1) object - (MyFRT *)createObject SWIFT_RETURNS_RETAINED;

// Returns an unretained (+0) object - (MyFRT *)getExistingObject SWIFT_RETURNS_UNRETAINED;

## 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)
