Sunday, March 19, 2017

Memory leak _ContiguousArrayStorage?

Leave a Comment

I'm new to swift.I'm running instrument tool for memory leaks.I found a leak "_ContiguousArrayStorage<String>"

Screen Shot of Instruments

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)) 

Screenshot of code leakage lines

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.

1 Answers

Answers 1

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 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment