# Capturing a noncopyable type in an async closure

**URL:** <https://forums.swift.org/t/capturing-a-noncopyable-type-in-an-async-closure/68118>\
**Category:** Using Swift\
**Tags:** concurrency, noncopyable\
**Created:** [October 27, 2023, 11:16pm UTC](https://forums.swift.org/t/capturing-a-noncopyable-type-in-an-async-closure/68118 "2023-10-27T23:16:24Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [October 30, 2023, 12:56am UTC](https://forums.swift.org/t/capturing-a-noncopyable-type-in-an-async-closure/68118/5 "2023-10-30T00:56:02Z")

</div>

i got this down to a minimal reproducer which i filed as a bug here:

> <https://github.com/apple/swift/issues/69496>
>
> the following test program crashes with a double free.
> 
> \`\`\`swift
> struct TimeC…op:~Copyable, Sendable
> {
> final
> class Class:Sendable
> {
> init()
> {
> }
> }
> 
> let x:Class
> 
> init()
> {
> self.x = .init()
> }
> 
> func use()
> {
> }
> 
> deinit
> {
> }
> }
> 
> @main
> enum Main
> {
> static
> func main() async throws
> {
> do
> {
> let cop:TimeCop = .init()
> async
> let \_:Void =
> {
> cop.use()
> try await Task.sleep(for: .milliseconds(50))
> }()
> }
> }
> }
> \`\`\`
> 
> \`\`\`
> $ swiftc -parse-as-library example.swift
> $ ./example 
> \`\`\`
> 
> \`\`\`
> 
> 💣 Program crashed: Bad pointer dereference at 0x0000000000000000
> 
> Thread 4 crashed:
> 
> 0 0x0000000000000000
> 1 0x00007fcf6be4c29b bool swift::RefCounts\<swift::RefCountBitsT\<(swift::RefCountInlinedness)1\> \>::doDecrementSlow\<(swift::PerformDeinit)1\>(swift::RefCountBitsT\<(swift::RefCountInlinedness)1\>, unsigned int) + 122 in libswiftCore.so
> 2 0x00005598a378b9c9 TimeCop.deinit + 24 in example
> 3 0x00005598a378bcd5 \_\_swift\_async\_resume\_get\_context + 36 in example
> 4 0x00007fcf6be4b8e0 \_swift\_release\_dealloc + 15 in libswiftCore.so
> 5 0x00007fcf6be4c29b bool swift::RefCounts\<swift::RefCountBitsT\<(swift::RefCountInlinedness)1\> \>::doDecrementSlow\<(swift::PerformDeinit)1\>(swift::RefCountBitsT\<(swift::RefCountInlinedness)1\>, unsigned int) + 122 in libswiftCore.so
> 6 0x00005598a378bc2e static Main.main() + 93 in example
> 7 0x00005598a378c600 static Main.$main() in example
> \`\`\`
> 
> \`\`\`
> $ swift --version
> Swift version 5.9.1 (swift-5.9.1-RELEASE)
> Target: x86\_64-unknown-linux-gnu
> \`\`\`

---

_[View the full topic](https://forums.swift.org/t/capturing-a-noncopyable-type-in-an-async-closure/68118)._
