Skip to content
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

Corrupted display name when passing c_str() to begin() #837

Open
kafana opened this issue Apr 13, 2024 · 2 comments
Open

Corrupted display name when passing c_str() to begin() #837

kafana opened this issue Apr 13, 2024 · 2 comments

Comments

@kafana
Copy link

kafana commented Apr 13, 2024

I am attempting to dynamically set up the display name using the homespan.begin(...) function. However, when I pass the c_str() of a String object into the begin function, it seems to affect the character pointer, resulting in the display name being corrupted. I assume that the begin() function should not modify the display name. This functionality was working correctly in version 1.8.

This is example code:

char nameSuffix[20];
sprintf(nameSuffix, "%06d", 101);
String displayName = "My Device Number " + String(nameSuffix);

...

homeSpan.begin(Category::Lighting, displayName.c_str());

The output looks something like this:

Accessory configuration number: 1

l��? is READY!

Trying to connect to XXXXXX_XXXXXX.  Waiting 1 sec...
Trying to connect to XXXXXX_XXXXXX.  Waiting 2 sec...
E (7391) wifi:sta is connecting, return error
WiFi Connected!  IP Address = xxx.21.31.205

Starting MDNS...

HostName:      HomeSpan-CAE729DD642F.local:80
Display Name:  l��?
Model Name:    HomeSpan-ESP32
Setup ID:      HSPN

Passing a constant string into begin() call works perfectly fine.

homeSpan.begin(Category::Lighting, "My Device 101");
My Device 101 is READY!

Trying to connect to XXXXXX_XXXXXX.  Waiting 1 sec...
WiFi Connected!  IP Address = xxx.21.31.205

Starting MDNS...

HostName:      HomeSpan-CAE729DD642F.local:80
Display Name:  My Device 101
Model Name:    HomeSpan-ESP32
Setup ID:      HSPN

Thank you for the amazing library!

@HomeSpan
Copy link
Owner

HomeSpan commented Apr 13, 2024

This is because the homeSpan.begin() function does not copy the full character array for any names passed to it (though I probably should do that in a future update). It only copies the pointers.

This means that if String is declared within setup(), it will go out of scope once the setup() function is completed by the time homeSpan.poll() is called within the main loop(), and the pointer to displayName will likely no longer be valid. Depending on the sketch, the memory occupied by displayName may or may not still contain a valid character array. If this worked in 1.8.0, it was only by luck.

The workaround is to define your String as a global variable instead of within setup so the name is retained and can be used once homeSpan.poll() is called. I'll see about reserving space to copy the full string in a future update.

@kafana
Copy link
Author

kafana commented Apr 14, 2024

This means that if String is declared within setup(), it will go out of scope once the setup() function is completed by the time homeSpan.poll() is called within the main loop(), and the pointer to displayName will likely no longer be valid. Depending on the sketch, the memory occupied by displayName may or may not still contain a valid character array. If this worked in 1.8.0, it was only by luck.

Thank you for the clarification. I've been using the same pattern for both serial numbers and pairing codes. I will transfer all related data to a global scope until a permanent fix is implemented.

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

No branches or pull requests

2 participants