# "pointer being freed was not allocated", unless I have an empty \`deinit\`

**URL:** <https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034>\
**Category:** Using Swift\
**Tags:** concurrency, crash\
**Created:** [January 9, 2026, 9:00am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034 "2026-01-09T09:00:00Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_L](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_l/32/44145_2.png) [@Chris\_L](https://forums.swift.org/u/Chris_L)\
**Post date:** [January 9, 2026, 9:00am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/1 "2026-01-09T09:00:00Z")

</div>

Hello,

I recently helped a colleague with an unexpected crash, which I have been able to reduce it to the code below.

Starting with a new iOS project, default settings, add these two files:

```swift
// Thing.swift
import Foundation

protocol Helper {
    func doStuff()
}

class StandardHelper: Helper {
    func doStuff() { }

    // deinit { }
}

struct OtherHelper: Helper {
    func doStuff() { }
}

@MainActor
class ThingWithInitWithDefault {
    private let helper: Helper

    // ⚠️ Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context
    init(helper: Helper = StandardHelper()) {
        self.helper = helper
    }

    func perform() {
        helper.doStuff()
    }
}

@MainActor
class ThingWithInitWithInternalDefault {
    private let helper: Helper

    init(helper: Helper? = nil) {
        // Brings the evaluation of the "default" *inside* our init
        self.helper = helper ?? StandardHelper()
    }

    func perform() {
        helper.doStuff()
    }
}

```

And:

```swift
// ThingsTests.swift
import Foundation
import XCTest
@testable import FreeingNeverAllocated

@MainActor
final class ThingTests: XCTestCase {
    // Crashes without empty deinit
    func testThingWithInitWithDefault() {
        let sut = ThingWithInitWithDefault()
        sut.perform()
    }

    // Crashes without empty deinit
    func testThingWithInitWithInternalDefault() {
        let sut = ThingWithInitWithInternalDefault()
        sut.perform()
    }

    // Always fine, because struct
    func testThingPassedHelperAtInit() {
        let sut = ThingWithInitWithDefault(helper: OtherHelper())
        sut.perform()
    }
}

```

The two `ThingWithInit*` implementations were to explore whether the issue was being caused by the need to evaluate a default value for an argument _before_ entering the `init` - it made no difference, but did clear the warning. Is there an annotation to address the warning in the first implementation?

With the code as-supplied, both of the first two tests will crash:

> malloc: \*\*\* error for object 0x262c4ff30: pointer being freed was not allocated

Uncommenting the empty `deinit` in `StandardHelper` stops the crash from happening.

(Confirmed on Xcode 26.1.2 and 26.0)

Following the recommendation to put a break point on `malloc_error_break` yields the following stack trace:

```swift
#0	0x000000018022ec64 in malloc_error_break ()
#1	0x000000018023c118 in malloc_vreport ()
#2	0x000000018023c36c in malloc_report ()
#3	0x0000000180208dc4 in ___BUG_IN_CLIENT_OF_LIBMALLOC_POINTER_BEING_FREED_WAS_NOT_ALLOCATED ()
#4	0x0000000257d38fcc in swift::TaskLocal::StopLookupScope::~StopLookupScope ()
#5	0x0000000257d2d3d8 in swift_task_deinitOnExecutorImpl ()
#6	0x000000010447ef5c in StandardHelper.__deallocating_deinit ()
#7	0x00000001972245d0 in _swift_release_dealloc ()
#8	0x0000000197225094 in swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>>::doDecrementSlow<(swift::PerformDeinit)1> ()
#9	0x000000010447f220 in __swift_destroy_boxed_opaque_existential_1 ()
#10	0x000000010447f3b0 in ThingWithInitWithDefault.deinit ()
#11	0x000000010447f3e8 in ThingWithInitWithDefault.__isolated_deallocating_deinit ()
#12	0x0000000257d2d3d0 in swift_task_deinitOnExecutorImpl ()
#13	0x000000010447f488 in ThingWithInitWithDefault.__deallocating_deinit ()
#14	0x00000001972245d0 in _swift_release_dealloc ()
#15	0x0000000197225094 in swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>>::doDecrementSlow<(swift::PerformDeinit)1> ()
#16	0x0000000105035310 in ThingTests.testThingWithInitWithDefault() at /Users/.../FreeingNeverAllocated/FreeingNeverAllocatedTests/ThingTests.swift:10

```

What are we doing wrong? The code isn’t doing anything complicated or obscure.

---

<div class="post-metadata">

**Author:** ![OneSadCookie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/onesadcookie/32/3205_2.png) [@OneSadCookie](https://forums.swift.org/u/OneSadCookie)\
**Post date:** [January 9, 2026, 9:39am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/2 "2026-01-09T09:39:55Z")

</div>

It looks like a bug in isolated deinit (@Nickolas_Pohilets), perhaps in combination with the mainactor-by-default feature. But I certainly don't understand why this code in particular causes a problem…

---

<div class="post-metadata">

**Author:** ![Nickolas\_Pohilets](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nickolas_pohilets/32/10659_2.png) [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Post date:** [January 9, 2026, 11:21am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/3 "2026-01-09T11:21:46Z")

</div>

For me `testThingPassedHelperAtInit` crashes as well.

Were you able to reproduce it on macOS? I would like to debug it using a debug build of standard library, but as far as I know it is not possible to run iOS apps using custom standard library.

---

<div class="post-metadata">

**Author:** ![Chris\_L](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_l/32/44145_2.png) [@Chris\_L](https://forums.swift.org/u/Chris_L)\
**Post date:** [January 9, 2026, 11:26am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/4 "2026-01-09T11:26:06Z")

</div>

Not yet, but I will try.

---

<div class="post-metadata">

**Author:** ![Nickolas\_Pohilets](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nickolas_pohilets/32/10659_2.png) [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Post date:** [January 9, 2026, 11:28am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/5 "2026-01-09T11:28:47Z")

</div>

I was able to reproduce on macOS. For both macOS and iOS issue reproduces if `Thing.swift` is inside a SwiftUI app, but does not reproduce if `Thing.swift` is inside a static library.

I'll make a debug build of the standard library and will try to debug it on macOS.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [January 9, 2026, 1:21pm UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/6 "2026-01-09T13:21:15Z")

</div>

> [@Nickolas\_Pohilets](#):
>
> I'll make a debug build of the standard library and will try to debug it on macOS.

It might be more fruitful to try a [swift.org](http://swift.org) toolchain built with asserts enabled instead (or build one yourself of course), because the SIL verifier and other assertions often catch issues with mismatched retain/release operations and other invariant violations.

---

<div class="post-metadata">

**Author:** ![Nickolas\_Pohilets](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nickolas_pohilets/32/10659_2.png) [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Post date:** [January 9, 2026, 1:34pm UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/7 "2026-01-09T13:34:51Z")

</div>

Looks like it was already fixed in [[Concurrency] Fix MarkerItem::create to use malloc when there's no task. · swiftlang/swift@29245e4 · GitHub](https://github.com/swiftlang/swift/commit/29245e4ef29d7cd039c31918993f7d25aae8382b)

---

<div class="post-metadata">

**Author:** ![Chris\_L](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_l/32/44145_2.png) [@Chris\_L](https://forums.swift.org/u/Chris_L)\
**Post date:** [January 9, 2026, 1:41pm UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/8 "2026-01-09T13:41:13Z")

</div>

That sounds promising. I guess it will be a month or two before we see it in an Xcode release.

For now, do you have any recommendation for an approach to avoid it? (i.e. in the sample code)

---

<div class="post-metadata">

**Author:** ![Nickolas\_Pohilets](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nickolas_pohilets/32/10659_2.png) [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Post date:** [January 9, 2026, 3:54pm UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/9 "2026-01-09T15:54:11Z")

</div>

Issues happens when deallocating in the presence of the task-local values. Wrapping code into `await Task.detached` should suppress it.

---

<div class="post-metadata">

**Author:** ![pkclsoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pkclsoft/32/44778_2.png) [@pkclsoft](https://forums.swift.org/u/pkclsoft)\
**Post date:** [March 14, 2026, 4:38am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/10 "2026-03-14T04:38:17Z")

</div>

I’ve just hit this after writing some reasonably simple classes to manage some data. As soon as I execute a test case, this exception happens. This is with latest Xcode (14/3/2026, Version 26.3 (17C529)).

So if I create empty deinit functions on each of these classes, there is no crash at the end of the unit test, but surely this is not a reasonable solution. I should be able to rely on the default deinit.

This application does not need to use async-await or anything like that; it’s going to be a straightforward SpriteKit app; no SwiftUI, nothing like it.

I have a similar project with unit tests that works just fine without the need to specify empty deinit functions. I checked it and it is explicitly using Swift5. I’ve tried this in the new project (created a week ago) and that made no difference.

Is there some other setting that may be new(ish) that would trigger this issue that I can alter?

---

<div class="post-metadata">

**Author:** ![OneSadCookie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/onesadcookie/32/3205_2.png) [@OneSadCookie](https://forums.swift.org/u/OneSadCookie)\
**Post date:** [March 17, 2026, 8:07am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/11 "2026-03-17T08:07:51Z")

</div>

> [@pkclsoft](#):
>
> Is there some other setting that may be new(ish) that would trigger this issue that I can alter?

Yes, the new setting for "approachable concurrency"

---

<div class="post-metadata">

**Author:** ![pkclsoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pkclsoft/32/44778_2.png) [@pkclsoft](https://forums.swift.org/u/pkclsoft)\
**Post date:** [March 17, 2026, 9:11am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/12 "2026-03-17T09:11:39Z")

</div>

Thanks for that. Sadly the state of this setting was “Yes” for both the macOS and test target. Turning it off for both makes absolutely no difference to the running of the unit tests. As soon as Xcode completes the tests for a class that has no `deinit` defined, the crash occurs.

---

<div class="post-metadata">

**Author:** ![snowG8](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@snowG8](https://forums.swift.org/u/snowG8)\
**Post date:** [March 18, 2026, 1:49pm UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/13 "2026-03-18T13:49:35Z")

</div>

I have this same issue, it happens 100% for some tests and for some tests no issues.  
xcode 26.2

For me fix was to make test cases “async”  
In your case - func testThingWithInitWithInternalDefault() async

---

<div class="post-metadata">

**Author:** ![pkclsoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pkclsoft/32/44778_2.png) [@pkclsoft](https://forums.swift.org/u/pkclsoft)\
**Post date:** [March 21, 2026, 2:32am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/14 "2026-03-21T02:32:14Z")

</div>

Thanks, I will give that a try.

---

<div class="post-metadata">

**Author:** ![pkclsoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pkclsoft/32/44778_2.png) [@pkclsoft](https://forums.swift.org/u/pkclsoft)\
**Post date:** [March 22, 2026, 3:23am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/15 "2026-03-22T03:23:13Z")

</div>

that did indeed work. its annoying that my project which uses swift 5.9 (def not 6+) seems to require the tests to support async for them to work unless I add those dumb empty deinit.

this whole thing feels like Apple making changes without adequate testing and consideration for the entire community of developers.

---

<div class="post-metadata">

**Author:** ![vplusm](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vplusm/32/44892_2.png) [@vplusm](https://forums.swift.org/u/vplusm)\
**Post date:** [March 27, 2026, 9:44am UTC](https://forums.swift.org/t/pointer-being-freed-was-not-allocated-unless-i-have-an-empty-deinit/84034/16 "2026-03-27T09:44:20Z")

</div>

It’s already working fine on Xcode 26.4
