Ios 修复反射库中的内存泄漏(?)
我们已经为我们的项目制作了一个反射库,我们在各种应用程序中使用它,我们有一个反复出现的崩溃,我们无法在内部重现 该库的目标是获取JSON输入(作为字典)并输出最终模型,而无需编写任何手动解析代码。因此,它使用了Swift的各种反射特性 自Swift 3.1以来,我们看到一些(更多)崩溃,原因如下: 对象xxxxxxxx的错误:无效指针从可用列表中退出队列Ios 修复反射库中的内存泄漏(?),ios,swift,reflection,Ios,Swift,Reflection,我们已经为我们的项目制作了一个反射库,我们在各种应用程序中使用它,我们有一个反复出现的崩溃,我们无法在内部重现 该库的目标是获取JSON输入(作为字典)并输出最终模型,而无需编写任何手动解析代码。因此,它使用了Swift的各种反射特性 自Swift 3.1以来,我们看到一些(更多)崩溃,原因如下: 对象xxxxxxxx的错误:无效指针从可用列表中退出队列 #1. Crashed: com.apple.root.default-qos 0 libsystem_kernel.dylib
#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)
这是BaseObject的setValue函数中代码的一部分,我们对所有JSON到本机对象使用基类
我们将此库用作网络后端的一部分,并且解析是在各种后台队列上进行的,因此可能是线程安全问题
我知道你没有太多的信息,但也许,也许有人有同样的问题,也许它在Swift库或编译器中,但我怀疑它;因为我不能复制它,所以我不能用僵尸来追踪它。
也许我们现在做的太脏了。。。还有更安全的方法 这里的上下文是什么?您在哪里使用fillArray?我猜您的问题是由于价值捕获问题,但没有上下文
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
}