Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-5145] Letting a long linked list go out of scope can cause a stack-overflow with ARC #47721

Open
weissi opened this issue Jun 7, 2017 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@weissi
Copy link
Member

weissi commented Jun 7, 2017

Previous ID SR-5145
Radar None
Original Reporter @weissi
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 4e989d478df2a592296a62e38448b82b

Issue Description:

this Swift program which creates a linked list with 500k nodes and then lets it go out of scope blows the stack with ARC as the deinit do recurse. Is this "working as expected"?

import Dispatch

public final class LinkedListWithoutData {
    public var next: LinkedListWithoutData?
}

func makeAndDestroy() -> Bool {
    let head = LinkedListWithoutData()

    var tail = head
    for _ in 0..<500_000 {
        let new = LinkedListWithoutData()
        tail.next = new
        tail = new
    }

    return head.next == nil
}

_ = makeAndDestroy()

Maybe I'm missing some cleaver workaround but I can only think of:

  • converting the list to an array and removing from the tail

  • doing something crazy like

    deinit {
        if #available(macOS 10.12, *) {
            if let next = self.next {
                DispatchQueue.global().async {
                    _ = next
                }
            }
        }
    }

haha

@belkadan
Copy link
Contributor

belkadan commented Jun 7, 2017

@atrick, @gparker42?

@atrick
Copy link
Member

atrick commented Jun 7, 2017

Unfortunately I think this is expected behavior. The only thing I can think of at the moment is explicitly clearing the list nonrecursively:

  deinit {
    var link = next
    while link != nil {
      let curr = link!
      link = curr.next
      curr.next = nil
    }
  }

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants