I am facing a memory leak which am unable to understand PFB
我正面临内存泄漏,无法理解PFB
Call trace:
调用跟踪:
- +0x196 callq "DYLD-STUB$$NSManagedObjectContext.fetch (NSFetchRequest) throws -> [A]"
- + 0 x196 callq”DYLD-STUB NSManagedObjectContext美元美元。获取(NSFetchRequest)抛出-> [A]"
- +0x8c callq "_arrayForceCast ([A]) -> [B]"
- +0x8c调用q "_arrayForceCast ([A]) -> [B]"
- +0xde callq "Collection.map ((A.Iterator.Element) throws -> A1) throws -> [A1]"
- + 0 xde callq”集合。map ((A.Iterator.Element)抛出-> A1)抛出-> [A1]
- +0x19e callq "ContiguousArray.reserveCapacity(Int) -> ()"
- +0x19e调用q " contiguousarray . reservation ecapacity (Int) -> ()"
- +0xaa callq "_ContiguousArrayBuffer.init(uninitializedCount : Int, minimumCapacity : Int) -> _ContiguousArrayBuffer"
- + 0 xaa _ContiguousArrayBuffer callq”。init(uninitializedCount: Int, minimumCapacity: Int) -> _ContiguousArrayBuffer”
- +0x42 callq "ManagedBufferPointer.init(_uncheckedBufferClass : AnyObject.Type, minimumCapacity : Int) -> ManagedBufferPointer"
- + 0×ManagedBufferPointer callq”。init(_uncheckedBufferClass:AnyObject。类型,最小容量:Int) -> ManagedBufferPointer
- +0x0f callq "swift_slowAlloc"
- + 0 x0f callq“swift_slowAlloc”
- +0x04 callq "DYLD-STUB$$malloc"
- + 0 x04 callq“DYLD-STUB $ $ malloc”
- +0x13 callq "malloc_zone_malloc"
- + 0 * 13 callq“malloc_zone_malloc”
- +0x8f movzbl 71543(%rip), %eax
- 71543 + 0 x8f movzbl(%撕裂),% eax
Edit:
编辑:
I Further investigated the code and i found out that the real leak is when i try to force cast type [Any] to [AnyObject] in coredata fetch request
我进一步研究了代码,发现真正的泄漏是当我试图强制在coredata获取请求中将类型[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
}
EDIT: @Jon,Kuba
编辑:@Jon,库巴地毯
Model.getEntities(entityType: EX_TEACHER.self, completion: {[unowned self] entityobjects in
self.teacherList = entityobjects
})
// in model class
/ /模型类
class func getEntities<T: NSManagedObject>(entityType: T.Type,completion: ([AnyObject]) -> Void)
{
let teacherList = coreDataOperation.fetchEntity(entityClass: entityType, setPredicate: nil)
completion(teacherList)
}
// cdh.managedObjectContext code
/ /鼎晖。获取代码
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
}() }
2 个解决方案
#1
6
After a lot of compilation and google search. i gave up and downloaded a new xcode version 8.3. (previous was 8.1)
经过大量的编译和谷歌搜索。我放弃了,下载了一个新的xcode版本8.3。(之前是8.1)
Then i retested the project and guess what.....? no leaks anymore. Well we can guess that it was xcode 8.1 version issues.
然后我重新测试了这个项目,猜猜看……没有泄漏了。我们可以猜到是xcode 8.1版本的问题。
EDIT:
编辑:
Debugging
调试
Resolved Issues The Memory Debugger for macOS and the iOS Simulator fixes reporting of false memory leaks for Swift classes containing either fields of type enum, or classes that inherit from certain Objective-C framework classes. (27932061) False reporting can still occur for iOS, watchOS, and tvOS applications. See (29335813) in Known Issues, below.
解决了macOS的内存调试器和iOS模拟器的问题,修复了包含枚举类型字段或继承了特定Objective-C框架类的类的Swift类的错误内存泄漏报告。(27932061) iOS、watchOS和tvOS应用程序仍然可能出现虚假报告。见(29335813)。
Known Issues The Memory Debugger for iOS, watchOS, and tvOS applications can report false memory leaks when debugging Swift classes containing either fields of type enum, or classes that inherit from certain Objective-C framework classes. (29335813)
已知的问题为iOS、watchOS和tvOS应用程序发出内存调试器,当调试包含enum类型或继承自特定Objective-C框架类的类的Swift类时,它们可以报告错误的内存泄漏。(29335813)
苹果Xcode 8.3发布说明
#2
1
Try this code
试试这个代码
do
{
var result: AnyObject = try cdh.managedObjectContext.fetch(fetchRequest)
let resultMirror = Mirror(reflecting: result)
print(resultMirror.subjectType) //gives you the return type from coredata
}catch let error as NSError
{
debugPrint("error in fetchrequest is",error)
result = []
}
#1
6
After a lot of compilation and google search. i gave up and downloaded a new xcode version 8.3. (previous was 8.1)
经过大量的编译和谷歌搜索。我放弃了,下载了一个新的xcode版本8.3。(之前是8.1)
Then i retested the project and guess what.....? no leaks anymore. Well we can guess that it was xcode 8.1 version issues.
然后我重新测试了这个项目,猜猜看……没有泄漏了。我们可以猜到是xcode 8.1版本的问题。
EDIT:
编辑:
Debugging
调试
Resolved Issues The Memory Debugger for macOS and the iOS Simulator fixes reporting of false memory leaks for Swift classes containing either fields of type enum, or classes that inherit from certain Objective-C framework classes. (27932061) False reporting can still occur for iOS, watchOS, and tvOS applications. See (29335813) in Known Issues, below.
解决了macOS的内存调试器和iOS模拟器的问题,修复了包含枚举类型字段或继承了特定Objective-C框架类的类的Swift类的错误内存泄漏报告。(27932061) iOS、watchOS和tvOS应用程序仍然可能出现虚假报告。见(29335813)。
Known Issues The Memory Debugger for iOS, watchOS, and tvOS applications can report false memory leaks when debugging Swift classes containing either fields of type enum, or classes that inherit from certain Objective-C framework classes. (29335813)
已知的问题为iOS、watchOS和tvOS应用程序发出内存调试器,当调试包含enum类型或继承自特定Objective-C框架类的类的Swift类时,它们可以报告错误的内存泄漏。(29335813)
苹果Xcode 8.3发布说明
#2
1
Try this code
试试这个代码
do
{
var result: AnyObject = try cdh.managedObjectContext.fetch(fetchRequest)
let resultMirror = Mirror(reflecting: result)
print(resultMirror.subjectType) //gives you the return type from coredata
}catch let error as NSError
{
debugPrint("error in fetchrequest is",error)
result = []
}