Skip to content

Fix Not by reference. But the value was updated. #3279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2020
Merged

Fix Not by reference. But the value was updated. #3279

merged 3 commits into from
Oct 3, 2020

Conversation

tanakamasayuki
Copy link
Contributor

No description provided.

retrieveCharacteristics();
}
log_v("<< getCharacteristics() for service: %s", getUUID().toString().c_str());
*pCharacteristicMap = m_characteristicMapByHandle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not by reference?

@tanakamasayuki
Copy link
Contributor Author

Now 1.0.3

void BLERemoteService::getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* pCharacteristicMap) {
	pCharacteristicMap = &m_characteristicMapByHandle;
}

Use case

std::map<uint16_t, BLERemoteCharacteristic*> *mapCharacteristics;
pRemoteService->getCharacteristics(mapCharacteristics);

mapCharacteristics is not update.
Because, pCharacteristicMap is copy value.(not reference)

Change to reference

void BLERemoteService::getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* &pCharacteristicMap) {
	if (!m_haveCharacteristics) {
		retrieveCharacteristics();
	}
	pCharacteristicMap = &m_characteristicMapByHandle;
}

Argument type : pCharacteristicMap -> &pCharacteristicMap
My recommendation.
But, The declaration changes.


I want m_characteristicMapByHandle.
Some BLE HID devices have multiple types of notifications with the same UUID.
 

@me-no-dev
Copy link
Member

pCharacteristicMap is a pointer to where m_characteristicMapByHandle is stored. This is what the & there means. Get the address of the object. Maybe the issue is elsewhere?

@tanakamasayuki
Copy link
Contributor Author

commnet

Now the value is copied and another variable is passed.
So even if you update, the original variable will not change.

Some countermeasure is required.

  • to reference
  • fix pointer
  • remove function

This function is difficult.
My recommendation is reference or remove.
But, Fix with less impact.

test code

int  a = 1;
int* b = NULL;
int  c = 0;

// now function
void func_copy( int *d ) {
  // d is copy value.(&d != &b)
  Serial.printf("func_copy\n");
  Serial.printf(" d:%p\n", d);
  Serial.printf(" &d:%p\n", &d);
  d = &a;
}

// use reference
void func_reference( int* &d ) {
  // d is original value.(&d == &b)
  Serial.printf("func_reference\n");
  Serial.printf(" d:%p\n", d);
  Serial.printf(" &d:%p\n", &d);
  d = &a;
}

// use pointer
void func_pointer( int *d ) {
  // d is original pointer address.(d == &b)
  Serial.printf("func_pointer\n");
  Serial.printf(" d:%p\n", d);
  Serial.printf(" &d:%p\n", &d);
  *d = a;
}

void setup() {
  Serial.begin(115200);
  delay(1000);

  // Init
  Serial.printf("Init\n");
  Serial.printf(" &a:%p\n", &a);
  Serial.printf(" &b:%p\n", &b);
  Serial.printf(" &c:%p\n", &c);
  Serial.printf("=================\n");

  // Now(copy value)
  b = NULL;
  func_copy(b);
  Serial.printf(" finish b:%p\n", b);

  Serial.printf("=================\n");

  // to reference
  b = NULL;
  func_reference(b);
  Serial.printf(" finish b:%p\n", b);

  Serial.printf("=================\n");

  // to pointer
  func_pointer(&c);
  Serial.printf(" finish c:%d\n", c);
}

void loop() {
}

output

Init
 &a:0x3ffbebe8
 &b:0x3ffc00c0
 &c:0x3ffc00bc
=================
func_copy
 d:0x0
 &d:0x3ffb1f5c
 finish b:0x0
=================
func_reference
 d:0x0
 &d:0x3ffc00c0
 finish b:0x3ffbebe8
=================
func_pointer
 d:0x3ffc00bc
 &d:0x3ffb1f5c
 finish c:1

And, warning output

C:\Users\tanaka\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src\BLERemoteService.cpp: In member function 'void BLERemoteService::getCharacteristics(std::map<short unsigned int, BLERemoteCharacteristic*>*)':

C:\Users\tanaka\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src\BLERemoteService.cpp:246:89: warning: parameter 'pCharacteristicMap' set but not used [-Wunused-but-set-parameter]

 void BLERemoteService::getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* pCharacteristicMap) {

                                                                                         ^

@me-no-dev me-no-dev merged commit 675a40b into espressif:master Oct 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants