Skip to content
This repository was archived by the owner on Jul 12, 2019. It is now read-only.

Stack overflow for reference cycle #9

Open
3 tasks done
broadwaylamb opened this issue Dec 1, 2016 · 2 comments
Open
3 tasks done

Stack overflow for reference cycle #9

broadwaylamb opened this issue Dec 1, 2016 · 2 comments
Labels

Comments

@broadwaylamb
Copy link
Collaborator

broadwaylamb commented Dec 1, 2016

New issue checklist

General information

  • Library version: 2.0.1

Expected behavior

The depth of the description of a reference cycled object should be limited.

Actual behavior

Obviously, it is not, which causes stack overflow.

Steps to reproduce

class A : CustomStringConvertible {
    var b: B
    init(b: B) {
        self.b = b
    }
}

class B : CustomStringConvertible {
    weak var a: A?
    init() {}
}

let b = B()
let a = A(b: b)
b.a = a

print(a.description)
@jessesquires
Copy link
Owner

lol

Good catch @broadwaylamb !

@broadwaylamb
Copy link
Collaborator Author

broadwaylamb commented Dec 5, 2016

@jessesquires well, I've just tried to fix it, but have come up with nothing.

The first idea that comes to mind is to add the currentDepth argument to the generateDefaultDescription and generateDeepDescription and decrease it when calling this function recursively. However we don't and can't always call it explicitly. For example, if an object contains a property of some value type, we cannot determine if this value type provides its description using DefaultStringConvertible implementation or its own, custom description. So we must fetch its description using not the generateDefaultDescription method, but String(describing:) function (or string interpolation, which is the equivalent). So depth counting using recursive calls with a decreasing currentDepth argument is quite difficult if not impossible.

Well we could use completely different solution, which is yet to be developed (and as I said I couldn't come up with one). But I'm afraid it won't be that elegant at all.

I feel that this situation is exactly the reason why classes don't provide any default description.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants