Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak _ContiguousArrayStorage?

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.

like image 879
Master Stroke Avatar asked Mar 10 '17 07:03

Master Stroke


People also ask

Is memory leak serious?

Very dangerous. Memory leaks in the kernel level lead to serious system stability issues. Kernel memory is very limited compared to user land memory and should be handled cautiously. Memory is allocated but never freed.

Can memory leaks damage RAM?

Memory leaks don't result in physical or permanent damage. Since it's a software issue, it will slow down the applications or even your whole system. However, a program taking up a lot of RAM space doesn't always mean its memory is leaking somewhere.

Is memory leak permanent?

Physical or permanent damage does not happen from memory leaks. Memory leaks are strictly a software issue, causing performance to slow down among applications within a given system. It should be noted a program taking up a lot of RAM space is not an indicator that memory is leaking.


1 Answers

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
like image 78
pwc Avatar answered Oct 11 '22 08:10

pwc