gpt4 book ai didi

ios - CoreData Fetch Request 将 [Any] 转换为 [AnyObject] 会产生内存泄漏

转载 作者:IT王子 更新时间:2023-10-29 05:36:50 24 4
gpt4 key购买 nike

我正面临无法理解的内存泄漏多溴联苯

enter image description here

通话记录:

  1. +0x196 callq "DYLD-STUB$$NSManagedObjectContext.fetch (NSFetchRequest) throws -> [A]"
  2. +0x8c callq "_arrayForceCast ([A]) -> [B]"
  3. +0xde callq "Collection.map ((A.Iterator.Element) throws -> A1) throws -> [A1]"
  4. +0x19e callq "ContiguousArray.reserveCapacity(Int) -> ()"
  5. +0xaa callq "_ContiguousArrayBuffer.init(uninitializedCount : Int, minimumCapacity : Int) -> _ContiguousArrayBuffer"
  6. +0x42 callq "ManagedBufferPointer.init(_uncheckedBufferClass : AnyObject.Type, minimumCapacity : Int) -> ManagedBufferPointer"
  7. +0x0f callq "swift_slowAlloc"
  8. +0x04 callq "DYLD-STUB$$malloc"
  9. +0x13 callq "malloc_zone_malloc"
  10. +0x8f movzbl 71543(%rip), %eax

enter image description here

enter image description here

编辑:

我进一步调查了代码,发现真正的泄漏是当我尝试在核心数据获取请求中强制将类型 [Any] 强制转换为 [AnyObject] 时

func fetchEntity<T: NSManagedObject>(entityClass:T.Type,setPredicate:NSPredicate?) -> [AnyObject]
{
let entityName = NSStringFromClass(entityClass)
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: entityName)
fetchRequest.predicate = setPredicate
fetchRequest.returnsObjectsAsFaults = false

var result:[AnyObject] = []
do
{
result = try cdh.managedObjectContext.fetch(fetchRequest) --> right here is the leak, when i cast the return object of [Any] to [AnyObject]

}catch let error as NSError
{
debugPrint("error in fetchrequest is",error)
result = []
}

return result
}

编辑:@乔恩,库巴

Model.getEntities(entityType: EX_TEACHER.self, completion: {[unowned self] entityobjects in
self.teacherList = entityobjects
})

//在模型类中

 class func getEntities<T: NSManagedObject>(entityType: T.Type,completion: ([AnyObject]) -> Void)
{
let teacherList = coreDataOperation.fetchEntity(entityClass: entityType, setPredicate: nil)

completion(teacherList)

}

//cdh.managedObjectContext代码

lazy var cdh:CoreDataStore = {
let cdh = CoreDataStore()
return cdh
}()

class CoreDataStore: NSObject{
lazy var managedObjectContext: NSManagedObjectContext = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}() }

最佳答案

经过大量编译和谷歌搜索。我放弃并下载了新的 xcode 8.3 版。 (之前是 8.1)

然后我重新测试了这个项目,你猜怎么着......?没有泄漏了。那么我们可以猜测这是 xcode 8.1 版本问题。

编辑:

调试

已解决的问题适用于 macOS 和 iOS 模拟器的内存调试器修复了包含枚举类型字段或从某些 Objective-C 框架类继承的类的 Swift 类的错误内存泄漏报告。 (27932061)iOS、watchOS 和 tvOS 应用程序仍然可能出现虚假报告。请参阅下面已知问题中的 (29335813)。

已知问题在调试包含枚举类型字段或从某些 Objective-C 框架类继承的类的 Swift 类时,适用于 iOS、watchOS 和 tvOS 应用程序的内存调试器可能会报告错误的内存泄漏。 (29335813)

apple Xcode 8.3 release notes

关于ios - CoreData Fetch Request 将 [Any] 转换为 [AnyObject] 会产生内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42642191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com