Swift 3 recursive enum with associated value memory error

Hi everyone,


Converted a small chunks of my project into Swift 3.


When I run the unit test, I run into some runtime error that seems to be related to memory (the crash doesn't happen all the time). I suspect it's related to recursive enum with associated values.


The definition of my enum is as follow:

indirect enum ContentURLSegment {
     case list(parentSeg: ContentURLSegment?, contentType: ContentType, page: Int)
     case detail(parentSeg: ContentURLSegment?, contentType: ContentType, serverID: UInt)
     case modifier(parentSeg: ContentURLSegment?, contentType: ContentType, modifier: String)
     case create(parentSeg: ContentURLSegment?, contentType: ContentType)
}


Can you guys recommend some ways where I can debug the problem? Or, isolate the problem to see if it's caused by my code or it's Swift 3 problem?


I tried checking "Zombie Objects" in "Diagnostics" tap in Scheme. But that doesn't seem to catch much extra information. (Maybe it only works with objc objects?)


Attached is the debug view when execution of the test suit stops.


Thanks for the help!

Bill


This is the link to the screenshot

In such a situation, I add Swift.print("var \(var)") in various places of code, here at each call of the enum, if possible with a counter to check how many times you get the recursion and when the crash ocurs.


Did the problem exist with Swift 2.2 ?

I'm getting a similar issue with Swift 3 in Xcode 8b2 after making a regular enum an indirect one:


indirect enum Action {
    case searchMovies(String?, [Movie])
    case error(Action, ErrorProtocol?)
}


After it became indirect, I was getting all kinds of memory crashes everywhere. Usually with this error:


malloc: *** error for object 0x108c1c7d8: pointer being freed was not allocated

Around here:

    0x1061bbe05 <+37>: callq  0x1061cf35c               ; symbol stub for: swift_unknownRelease
    0x1061bbe0a <+42>: movq   -0x10(%rbp), %rax
    0x1061bbe0e <+46>: movq   0x20(%rax), %rdi
    0x1061bbe12 <+50>: callq  0x1061cf2ea               ; symbol stub for: swift_bridgeObjectRelease
    0x1061bbe17 <+55>: movl   $0x38, %ecx


Which leads me to believe there are some memory management issues with indirect enums.


I reverted back to regular enums and the memory crashes disappeared.

Thanks for posting your situation yood. The disassemble of my case looks like this...

    0x1034227a3 <+211>: jne    0x1034227be               ; <+238> at URLCommand.swift:84
    0x1034227a5 <+213>: movabsq $0x3fffffffffffffff, %rax ; imm = 0x3FFFFFFFFFFFFFFF
    0x1034227af <+223>: movq   -0x40(%rbp), %rcx
    0x1034227b3 <+227>: andq   %rax, %rcx
    0x1034227b6 <+230>: movq   %rcx, %rdi
    0x1034227b9 <+233>: callq  0x103421430               ; rt_swift_release
->  0x1034227be <+238>: movq   -0x38(%rbp), %rdi
    0x1034227c2 <+242>: callq  0x103421430               ; rt_swift_release
    0x1034227c7 <+247>: movb   -0x42(%rbp), %al
    0x1034227ca <+250>: movzbl %al, %edi


The swift code looks harmless to me...

  var page: Int? {
       if case let .list(_, contentType, myPage) = self where contentType.supportsPagination {
            return myPage
       }
       return nil
  }
Swift 3 recursive enum with associated value memory error
 
 
Q