mercredi 10 mai 2017

Fix memory leak (?) in a reflection library

We've made a reflection library for our projects, we use it in various apps and we have a recurring crash we can't reproduce internally.

The goal of the library is to take a JSON input (as a dictionary) and output a final model without writing any manual parsing code. So it use the various reflection features of Swift.

Since Swift 3.1 we see some (more) crash on this with the reason: error for object xxxxxxxx: Invalid pointer dequeued from free list

#1. Crashed: com.apple.root.default-qos
0  libsystem_kernel.dylib         0x183941014 __pthread_kill + 8
1  libsystem_pthread.dylib        0x183a0b334 pthread_kill + 112
2  libsystem_c.dylib              0x1838b59c4 abort + 140
3  libsystem_malloc.dylib         0x1839869e8 nanozone_default_reader + 330
4  libsystem_malloc.dylib         0x183987c54 _nano_malloc_check_clear + 412
5  libsystem_malloc.dylib         0x183986c38 nano_malloc + 44
6  libsystem_malloc.dylib         0x183975664 malloc_zone_malloc + 172
7  libsystem_malloc.dylib         0x18397856c malloc + 32
8  libswiftCore.dylib             0x100c20050 swift_slowAlloc (__hidden#19907_:29)
9  libswiftCore.dylib             0x100c200a4 _hidden#19910_ (__hidden#19936_:90)
10 libswiftCore.dylib             0x100ab2dbc _NativeDictionaryBuffer<A, B where ...>.init(capacity : Int) -> _NativeDictionaryBuffer<A, B> (__hidden#13751_:5395)
11 libswiftCore.dylib             0x100ab5318 _VariantDictionaryBuffer.ensureUniqueNativeBuffer(Int) -> (reallocated : Bool, capacityChanged : Bool) (__hidden#13751_:5384)
12 libswiftCore.dylib             0x100ab6334 _VariantDictionaryBuffer.nativeUpdateValue(B, forKey : A) -> B? (__hidden#13751_:6383)
13 libswiftCore.dylib             0x100aa3c0c _VariantDictionaryBuffer.updateValue(B, forKey : A) -> B? + 28
14 libswiftFoundation.dylib       0x100eee82c _hidden#368_ (__hidden#3515_)
15 libswiftFoundation.dylib       0x100eeb13c _hidden#350_ (__hidden#3515_)
16 libswiftFoundation.dylib       0x100eea6c4 static Dictionary._conditionallyBridgeFromObjectiveC(NSDictionary, result : inout [A : B]?) -> Bool (__hidden#3515_:106)
17 libswiftCore.dylib             0x100c059c4 _hidden#19215_ (__hidden#19254_:2885)
18 libswiftCore.dylib             0x100c04070 swift_dynamicCast (__hidden#19254_:2456)
19 libswiftCore.dylib             0x100c05adc _hidden#19216_ (__hidden#19254_:1593)
20 libswiftCore.dylib             0x100c03b70 swift_dynamicCast (__hidden#19254_:2463)
21 libswiftFoundation.dylib       0x100f15098 _hidden#614_ (__hidden#3515_)
22 libswiftFoundation.dylib       0x100ece79c _hidden#219_ (__hidden#3515_)
23 libswiftFoundation.dylib       0x100ee565c _hidden#319_ (__hidden#3515_)
24 libswiftFoundation.dylib       0x100ee55a0 static Array._conditionallyBridgeFromObjectiveC(NSArray, result : inout [A]?) -> Bool (__hidden#3515_:83)
25 libswiftCore.dylib             0x100c059c4 _hidden#19215_ (__hidden#19254_:2885)
26 libswiftCore.dylib             0x100c04070 swift_dynamicCast (__hidden#19254_:2456)
27 libswiftCore.dylib             0x100c05b18 _hidden#19216_ (__hidden#19254_:1593)
28 libswiftCore.dylib             0x100c03b70 swift_dynamicCast (__hidden#19254_:2463)
29 Redacted                        0x1005353a0 specialized Redacted.(setValue(Any!, forKey : String) -> ()).(closure #1) (Redacted.swift:167)

The line 167 of our Library "Redacted.swift" is

       if value is [AnyObject] {
            let objectValue = self.value(forKey: key)
           (167) let fillArray = {(eType: BaseObject.Type) in
                var newArray: [BaseObject] = []
                if let value = value as? [[String: AnyObject]] {
                    newArray = value.map({ (dico) -> BaseObject in
                        return eType.init(dictionary: dico)
                    })
                }
                newValue = newArray
            }

This is a part of our code within the setValue function of the BaseObject we use a Base class for all of our JSON to native object.

We use this library as a part of our network backend, and the parsing is made on various background queues, so maybe it's a thread safety issue.

I know you don't have a lot of information, but maybe, maybe someone have the same issue, maybe it's within the Swift library or compiler but I doubt it; Since I can't reproduce it I can't really track it with Zombies. Maybe what we're doing is too dirty too... and there are safer ways.





Aucun commentaire:

Enregistrer un commentaire