内存泄漏_ContiguousArrayStorage?

Mas*_*oke 7 iphone xcode memory-leaks ios swift

我是swift的新手.我正在运行内存泄漏的仪器工具.我发现了泄漏 "_ContiguousArrayStorage<String>"

屏幕截图的仪器

它导致下面部分代码

let myData = NSData(data: request.value!)
let buffer = Array(UnsafeBufferPointer(start: UnsafePointer<CUnsignedChar>(myData.bytes), count: myData.length))
Run Code Online (Sandbox Code Playgroud)

代码泄漏线的屏幕截图

谁能帮我吗?.

上面的代码有什么问题吗?

编辑:添加更多代码.

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])
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

pwc*_*pwc 1

看起来您正在尝试将 转换NSData为 的数组CUnsignedChar。您不需要使用UnsafeBufferPointerUnsafePointer执行该转换。我怀疑您使用不安全指针是内存泄漏的根本原因。

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
Run Code Online (Sandbox Code Playgroud)