Post

Replies

Boosts

Views

Activity

CoreBluetooth writeValue:forCharacteristic:type: retains the data
For a personal project, I have been writing some library to interface the CoreBluetooth API with the go language. Because of what it does, that library is written in objective-C with ARC disabled. One function I had to write takes a memory buffer (allocated with malloc() on the go side), creates an NSData object out of it to pass the data to the writeValue:forCharacteristic:type: CoreBluetooth method, and then releases the NSData as well as the original memory buffer: void Write(CBPeripheral *p, CBCharacteristic *c, void *bytes, int len) { NSData *data = [NSData dataWithBytesNoCopy:bytes length:len freeWhenDone:true]; [p writeValue:data forCharacteristic:c type:CBCharacteristicWriteWithoutResponse]; [data release]; } One thing I noticed is that the retainCount for data increases during the writeValue:forCharacteristic:type: API call. It is 1 before, and 2 after. This is surprising to me, because the documentation says "This method copies the data passed into the data parameter, and you can dispose of it after the method returns." I suspects this results in a memory leak. Am I missing something here ?
4
0
254
Oct ’24