簡體   English   中英

使用'case let x = y作為NSString導致內存泄漏

[英]memory leak using 'case let x = y as NSString

我使用的是帶有保護套的開關,但發現它泄漏了內存。 請參閱下面的儀器屏幕截圖。 為什么開關盒結構會給我帶來內存泄漏?

導致內存泄漏的代碼:

switch theValue {
// Bool, Int, UInt, Float and Double are casted to NSNumber by default!
case let numValue as NSNumber:
    return (numValue, "NSNumber", false)
case let longValue as Int64:
    return (NSNumber(longLong: longValue), "NSNumber", false)
case let longValue as UInt64:
    return (NSNumber(unsignedLongLong: longValue), "NSNumber", false)
case let intValue as Int32:
    return (NSNumber(int: intValue), "NSNumber", false)
case let intValue as UInt32:
    return (NSNumber(unsignedInt: intValue), "NSNumber", false)
case let intValue as Int16:
    return (NSNumber(short: intValue), "NSNumber", false)
case let intValue as UInt16:
    return (NSNumber(unsignedShort: intValue), "NSNumber", false)
case let intValue as Int8:
    return (NSNumber(char: intValue), "NSNumber", false)
case let intValue as UInt8:
    return (NSNumber(unsignedChar: intValue), "NSNumber", false)
case let stringValue as NSString:
    return (stringValue, "NSString", false)
case let stringValue as String:
    return (NSString(string: stringValue), "NSString", false)
case let dateValue as NSDate:
    return (dateValue, "NSDate", false)
case let anyvalue as NSArray:
    return (anyvalue, valueType, false)
case let anyvalue as EVObject:
    if valueType.containsString("<") {
        return (anyvalue, swiftStringFromClass(anyvalue), true)
    }
    return (anyvalue, valueType, true)
case let anyvalue as NSObject:
    if valueType.containsString("<") {
        return (anyvalue, swiftStringFromClass(anyvalue), false)
    }
    // isObject is false to prevent parsing of objects like CKRecord, CKRecordId and other objects.
    return (anyvalue, valueType, false)
default:
    (parentObject as? EVObject)?.addStatusMessage(.InvalidType, message: "valueForAny unkown type \(valueType) for value: \(theValue).")
    print("ERROR: valueForAny unkown type \(valueType) for value: \(theValue).")
    return (NSNull(), "NSNull", false)
}

將其轉換為以下內容后,內存泄漏消失了:

if theValue is NSNumber {
    return (theValue as! NSNumber, "NSNumber", false)
}
if theValue is Int64 {
    return (NSNumber(longLong: theValue as! Int64), "NSNumber", false)
}
if theValue is UInt64 {
    return (NSNumber(unsignedLongLong: theValue as! UInt64), "NSNumber", false)
}
if theValue is Int32 {
    return (NSNumber(int: theValue as! Int32), "NSNumber", false)
}
if theValue is UInt32 {
    return (NSNumber(unsignedInt: theValue as! UInt32), "NSNumber", false)
}
if theValue is Int16 {
    return (NSNumber(short: theValue as! Int16), "NSNumber", false)
}
if theValue is UInt16 {
    return (NSNumber(unsignedShort: theValue as! UInt16), "NSNumber", false)
}
if theValue is Int8 {
    return (NSNumber(char: theValue as! Int8), "NSNumber", false)
}
if theValue is UInt8 {
    return (NSNumber(unsignedChar: theValue as! UInt8), "NSNumber", false)
}
if theValue is NSString {
    return (theValue as! NSString, "NSString", false)
}
if theValue is NSDate {
    return (theValue as! NSDate, "NSDate", false)
}
if theValue is NSArray {
    return (theValue as! NSArray, valueType, false)
}
if theValue is EVObject {
    if valueType.containsString("<") {
        return (theValue as! EVObject, swiftStringFromClass(theValue as! EVObject), true)
    }
    return (theValue as! EVObject, valueType, true)
}
if theValue is NSObject {
    if valueType.containsString("<") {
        return (theValue as! NSObject, swiftStringFromClass(theValue as! NSObject), true)
    }
    return (theValue as! NSObject, valueType, true)
}
(parentObject as? EVObject)?.addStatusMessage(.InvalidType, message: "valueForAny unkown type \(valueType) for value: \(theValue).")
print("ERROR: valueForAny unkown type \(valueType) for value: \(theValue).")
return (NSNull(), "NSNull", false)

這是屏幕截圖,您可以在其中查看有關泄漏的詳細信息:

在此處輸入圖片說明

這是一個已知的問題。 最初,它應該已經在Xcode 7.1中修復,但是顯然仍然存在問題。

該錯誤在這里報告: https : //bugs.swift.org/browse/SR-1339?jql=text%20~%20%22switch%20case%20leak%22

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM