Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions documentation/cxx-interop/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,30 @@ object.doSomething()
// `object` will be released here.
```

To specify the ownership of returned `SWIFT_SHARED_REFERENCE` types, use the `SWIFT_RETURNS_RETAINED` and `SWIFT_RETURNS_UNRETAINED` annotations on C++ functions and methods. These annotations tell the Swift compiler whether the type is returned as `+1` (retained) or `+0` (unretained). This is necessary to ensure that appropriate `retain`/`release` operations are inserted at the boundary:

```c++
// Returns +1 ownership; Swift will take responsibility for releasing it.
SharedObject* makeOwnedObject() SWIFT_RETURNS_RETAINED;

// Returns +0 ownership; the caller must ensure the object stays alive.
SharedObject* makeUnownedObject() SWIFT_RETURNS_UNRETAINED;
```

These ownership conventions are reflected naturally in Swift:
```swift
let owned = makeOwnedObject()
owned.doSomething()
// `owned` is automatically released when it goes out of scope

let unowned = makeUnownedObject()
unowned.doSomething()
// make sure `unowned` remains valid while in use
```

These ownership annotations are also supported in Objective-C or Objective-C++ functions that return C++ `SWIFT_SHARED_REFERENCE` types.


### Inheritance and Virtual Member Functions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: ### -> ####


Similar to value types, casting an instance of a derived reference type to a
Expand Down