# \#deinit

**URL:** https://forums.swift.org/tag/deinit/476.md

[Latest](https://forums.swift.org/latest.md) · [Categories](https://forums.swift.org/categories.md) · [Tags](https://forums.swift.org/tags.md)

---

## [Lazy NSObject + deinit = trap](https://forums.swift.org/t/lazy-nsobject-deinit-trap/89879)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 4\
**Last updated:** [November 17, 2026, 5:59pm UTC](https://forums.swift.org/t/lazy-nsobject-deinit-trap/89879 "2026-11-17T17:59:20Z")

</div>

Found an interesting foot-gun (only traps with NSObject classes and only with Foundation on Apple platforms): import Foundation class C: NSObject { lazy var foo: Int = { // Thread 1: abort with payload or r…

---

## [SE-0544: Mutation and consumption in non-Copyable type deinits](https://forums.swift.org/t/se-0544-mutation-and-consumption-in-non-copyable-type-deinits/88903)

<div class="topic-metadata">

**Author:** [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Replies:** 3\
**Last updated:** [August 25, 2026, 10:53pm UTC](https://forums.swift.org/t/se-0544-mutation-and-consumption-in-non-copyable-type-deinits/88903 "2026-08-25T22:53:57Z")

</div>

Hello, Swift community. The review of SE-0544: Mutation and consumption in non-Copyable type deinits begins now and runs through August 24th, 2026. Reviews are an important part of the Swift evolution process. All revi…

---

## [Possible double-destruction bug for ~Copyable type when targeting WASM](https://forums.swift.org/t/possible-double-destruction-bug-for-copyable-type-when-targeting-wasm/85078)

<div class="topic-metadata">

**Author:** [@joehink](https://forums.swift.org/u/joehink)\
**Replies:** 6\
**Last updated:** [March 8, 2026, 11:25am UTC](https://forums.swift.org/t/possible-double-destruction-bug-for-copyable-type-when-targeting-wasm/85078 "2026-03-08T11:25:32Z")

</div>

I’m seeing different destruction behavior for a move-only type (~Copyable) when compiling for native vs WebAssembly. Native behavior matches expectations, but the WASM build appears to destroy the value twice. struct S…

---

## [Accessing sendable parts of non-sendable tuples within deinit](https://forums.swift.org/t/accessing-sendable-parts-of-non-sendable-tuples-within-deinit/78402)

<div class="topic-metadata">

**Author:** [@Karl](https://forums.swift.org/u/Karl)\
**Replies:** 1\
**Last updated:** [March 10, 2025, 7:58am UTC](https://forums.swift.org/t/accessing-sendable-parts-of-non-sendable-tuples-within-deinit/78402 "2025-03-10T07:58:47Z")

</div>

I would like to write something like the following: final class NonSendable {} @MainActor final class Foo { var stream: (source: NonSendable, consumer: Task\<Void, any Error\>)? deinit { stream?.consumer.cancel…

---

## [Versioning & back deployment of isolated deinit](https://forums.swift.org/t/versioning-back-deployment-of-isolated-deinit/76089)

<div class="topic-metadata">

**Author:** [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Replies:** 10\
**Last updated:** [January 8, 2025, 12:53pm UTC](https://forums.swift.org/t/versioning-back-deployment-of-isolated-deinit/76089 "2025-01-08T12:53:48Z")

</div>

PR that was making isolated deinit non-experiment, and which I hoped will be the last one, got reverted, because tests were failing on iOS device. Which I strongly suspect is just a matter of correct versioning. Tests …

---

## [Deinit does not support async / @MainActor](https://forums.swift.org/t/deinit-does-not-support-async-mainactor/76466)

<div class="topic-metadata">

**Author:** [@ola](https://forums.swift.org/u/ola)\
**Replies:** 7\
**Last updated:** [December 8, 2024, 6:14pm UTC](https://forums.swift.org/t/deinit-does-not-support-async-mainactor/76466 "2024-12-08T18:14:36Z")

</div>

I am using SwiftData without using an app and it does not save automatically when it quits (.autosaveEnabled==true), so I thought I could call .mainContext.save() from the deinit of the swiftdata-owner, but it has to run…

---

## [Why would \`deinit\` be called when retain count is non-zero?](https://forums.swift.org/t/why-would-deinit-be-called-when-retain-count-is-non-zero/72924)

<div class="topic-metadata">

**Author:** [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Replies:** 12\
**Last updated:** [July 4, 2024, 5:05am UTC](https://forums.swift.org/t/why-would-deinit-be-called-when-retain-count-is-non-zero/72924 "2024-07-04T05:05:04Z")

</div>

I'm seeing a rather surprising behaviour, which is that a Swift class is being deallocated (and deinit called) while it is retained (by multiple objects, which are themselves live and not going away). What can cause tha…

---

## [What is "reference may have escaped from deinit"?](https://forums.swift.org/t/what-is-reference-may-have-escaped-from-deinit/64866)

<div class="topic-metadata">

**Author:** [@xaphod](https://forums.swift.org/u/xaphod)\
**Replies:** 9\
**Last updated:** [April 5, 2024, 9:49am UTC](https://forums.swift.org/t/what-is-reference-may-have-escaped-from-deinit/64866 "2024-04-05T09:49:24Z")

</div>

Hi i'm trying to serialize some work on queues and i'm getting a crash with this error. I'm not sure what it means, and i'm not sure where the crash site actually is. Can someone help me understand what this is? thanks! …

---

## [Async deinit diagnostics in swift-syntax](https://forums.swift.org/t/async-deinit-diagnostics-in-swift-syntax/65096)

<div class="topic-metadata">

**Author:** [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Replies:** 2\
**Last updated:** [June 11, 2023, 2:23pm UTC](https://forums.swift.org/t/async-deinit-diagnostics-in-swift-syntax/65096 "2023-06-11T14:23:53Z")

</div>

I’m working on adding support for parsing async deinit in swift-syntax, but I’m a bit stuck with producing diagnostics. For which cases does it make sense to produce customised diagnostics? These two probably are must h…

---

## [Why @objc deinit uses ObjC messages to call super deinit?](https://forums.swift.org/t/why-objc-deinit-uses-objc-messages-to-call-super-deinit/64435)

<div class="topic-metadata">

**Author:** [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Replies:** 1\
**Last updated:** [April 16, 2023, 7:24pm UTC](https://forums.swift.org/t/why-objc-deinit-uses-objc-messages-to-call-super-deinit/64435 "2023-04-16T19:24:59Z")

</div>

Consider the following example: import Foundation func qux() { print("Q\_U\_X") } func zab() { print("Z\_A\_B") } class Foo: NSObject { deinit { qux() } } class Bar: Foo { deinit { zab() } } Code gen…

---

## [Unit testing memory leakage with actors](https://forums.swift.org/t/unit-testing-memory-leakage-with-actors/56953)

<div class="topic-metadata">

**Author:** [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Replies:** 0\
**Last updated:** [April 25, 2022, 8:30am UTC](https://forums.swift.org/t/unit-testing-memory-leakage-with-actors/56953 "2022-04-25T08:30:31Z")

</div>

I'm trying to write some unit tests that verify that certain actors do not retain themselves upon being initialized. The first test I wrote was giving me intermittent results (if I run it 10 times it retains itself 5 tim…

---

## [Retain count set to 2 and no deinit called](https://forums.swift.org/t/retain-count-set-to-2-and-no-deinit-called/43571)

<div class="topic-metadata">

**Author:** [@Erick](https://forums.swift.org/u/Erick)\
**Replies:** 30\
**Last updated:** [January 12, 2021, 9:03am UTC](https://forums.swift.org/t/retain-count-set-to-2-and-no-deinit-called/43571 "2021-01-12T09:03:20Z")

</div>

This rabbit hole is way bigger, but, in the mean time, I have two questions: Why is the deinit code never called here? Why is the retain count returned: 2? Code is below and compiled in macOS Big Sur with the latest e…
