Skip to content

[SR-15120] Using Threads in Swift causes memory leak #3949

@swift-ci

Description

@swift-ci
Previous ID SR-15120
Radar None
Original Reporter grill2010 (JIRA User)
Type Bug
Status Closed
Resolution Invalid

Attachment: Download

Environment

I'm using

XCode 12.5.1

Swift 5.5

Minimum iOS version

13.0

The app is built for iPhone and iPad

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 54829ec4c92d275e0feacca6ee17bd9b

Issue Description:

I'm creating a Thread in my ViewModel in Swift like this

import Foundation

class TestViewViewModel: ObservableObject {
    
    private var testThread: TestThread?
    
    public func initViewModel() {
        testThread = TestThread.init()
    }
    
    public func deinitViewModel() {
        testThread?.cancel()
        testThread = nil
    }
    
    
    /*+++++++++++++++*/
    /*+ Test thread +*/
    /*+++++++++++++++*/

    private class TestThread: Thread {

        override init(){
            print("Init RenderThread")
        }
        
        deinit {
            print("deinit RenderThread")
        }

        override func main() {
            
        }
    }
}

I'm not even starting the thread but even after calling deinitViewModel some stuff of the Thread got leaked according to the XCode memory profiler (look at the attached screenshot). It seems it's the threadDictionary but I'm not sure.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions