I'm new to swift.I'm running instrument tool for memory leaks.I found a leak "_ContiguousArrayStorage<String>"
Its leading to below part of the code
let myData = NSData(data: request.value!)
let buffer = Array(UnsafeBufferPointer(start: UnsafePointer<CUnsignedChar>(myData.bytes), count: myData.length))
Can anyone help me out?.
Anything wrong with above code?
Edit: Adding some more code.
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])
The parseData method of the singleton class returns NSMutableDictionary.
func parseData(responseData: [CUnsignedChar]) -> NSMutableDictionary
{
let infoDictionary = NSMutableDictionary()
let subIndexValue = Int(responseData[5])
infoDictionary.setValue(subIndexValue, forKey: KEY_SUB_INDEX)
return responseData
}
Thanks in advance.
It looks like you are trying to convert the NSData
to an array of CUnsignedChar
. You don't need to use UnsafeBufferPointer
or UnsafePointer
to do that conversion. I suspect your use of the unsafe pointers is the root cause of your memory leak.
You can just create the array by passing it a Data
object instead of an NSData
object. Try this instead:
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
I had the same problem on a project that was NOT a mixture of Objective-C and Swift. It turned out that string interpolation can leak memory:
http://www.openradar.me/26761490
The TL;DR is to update your string interpolations to string concatenations instead.
So:
print("Cannot write to characteristic \(request.characteristic.UUID)")
Becomes:
print("Cannot write to characteristic " + request.characteristic.UUID)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.