esp-nimble-cpp icon indicating copy to clipboard operation
esp-nimble-cpp copied to clipboard

Return a pointer to the Value object

Open tenderlove opened this issue 2 years ago • 3 comments

Otherwise we get a copy, and the copy seems to clobber the data we want

Before this commit, calling getValue() would return a copy of the underlying NimBLEAttValue object. It seems like the copy doesn't necessarily copy the underlying data stored in the value. In this commit, I changed it to just return a pointer (which fixes my application), but maybe the class should know how to copy its underlying data.

I'm not sure if this is the best fix (I'm not a pro C++ developer), but as I said this fixes my app.

Thanks!

tenderlove avatar Feb 13 '23 22:02 tenderlove

Providing a pointer to the underlying data structure is dangerous because it could change at any time, thus the reason for the copy. Could you provide an example of the data being "clobbered" to help find the cause?

h2zero avatar May 29 '23 14:05 h2zero

@h2zero (I need to preface this by saying I'm not a BLE expert, nor C++ expert). My application is trying to read HID data written to USB from my computer. I'm using libusb to send data to the bluetooth device. I think the problem is that since you can send an arbitrary number of bytes from the computer to the bluetooth device, doing the copy doesn't work as it doesn't know how many bytes to copy (since it's not known at compile time).

I'm using the pointer here.

Without this change, I get an object back with not clobbered, but uninitialized memory (sorry I should have been more clear in the original post).

tenderlove avatar May 24 '24 18:05 tenderlove

Thank you for replying, that is certainly odd. I do not see how it would be possible for the data to be uninitialized. If you changed your code to NimBLEAttValue buff = me->getValue(); it should copy and work in your code as is because it has a copy operator defined.

h2zero avatar Jun 03 '24 20:06 h2zero

Closing this as it's not necessary.

h2zero avatar Oct 13 '24 20:10 h2zero