繁体   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