[英]Memory leak _ContiguousArrayStorage?
我是swift的新手。我正在运行内存泄漏的仪器工具。我发现了泄漏"_ContiguousArrayStorage<String>"
它导致下面部分代码
let myData = NSData(data: request.value!)
let buffer = Array(UnsafeBufferPointer(start: UnsafePointer<CUnsignedChar>(myData.bytes), count: myData.length))
谁能帮我吗?。
上面的代码有什么问题吗?
编辑:添加更多代码。
let myData = NSData(data: request.value!)
var buffer = Array(UnsafeBufferPointer(start: UnsafePointer<CUnsignedChar>(myData.bytes), count: myData.length))
let responseArray: [CUnsignedChar] = Array(buffer)
let responseValue = BluetoothCommunicationManager.sharedInstance.parseData(responseArray,length: myData.length).0
let responseName = BluetoothCommunicationManager.sharedInstance.parseData(responseArray,length: myData.length).1
NSNotificationCenter.defaultCenter().postNotificationName(responseName, object: request, userInfo: responseValue as [NSObject : AnyObject])
singleton类的parseData方法返回NSMutableDictionary。
func parseData(responseData: [CUnsignedChar]) -> NSMutableDictionary
{
let infoDictionary = NSMutableDictionary()
let subIndexValue = Int(responseData[5])
infoDictionary.setValue(subIndexValue, forKey: KEY_SUB_INDEX)
return responseData
}
提前致谢。
看起来您正在尝试将NSData
转换为CUnsignedChar
数组。 您不需要使用UnsafeBufferPointer
或UnsafePointer
来执行该转换。 我怀疑你使用不安全的指针是你的内存泄漏的根本原因。
您可以通过传递Data
对象而不是NSData
对象来创建数组。 试试这个:
let myData = NSData(data: request.value!) as Data
let responseArray = [CUnsignedChar](myData)
let responseValue = BluetoothCommunicationManager.sharedInstance.parseData(responseArray, length: responseArray.count).0
let responseName = BluetoothCommunicationManager.sharedInstance.parseData(responseArray, length: responseArray.count).1
我在一个不是Objective-C和Swift混合的项目上遇到了同样的问题。 原来,字符串插值可能会泄漏内存:
http://www.openradar.me/26761490
TL; DR用于将字符串插值更新为字符串连接。
所以:
print("Cannot write to characteristic \(request.characteristic.UUID)")
变为:
print("Cannot write to characteristic " + request.characteristic.UUID)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.