Possible bug with saving Device name from

There seems to be a bug with storing the device name. I followed the docs https://docs.particle.io/reference/firmware/photon/#get-device-name and all seemed to work well. However, when I go to store the data into a string so that I can use it later something goes wrong.

String unitName;
void handler(const char *topic, const char *data) {
    Particle.publish("dName","received " + String(topic) + ": " + String(data),PRIVATE); // Might cause a bug with storing the name.
    unitName = String(data);
    Particle.publish("uName",unitName,PRIVATE);
}

The above code results in.

However, if I save the name before publishing it. All is well

String unitName;
void handler(const char *topic, const char *data) {
    unitName = String(data);
    Particle.publish("dName","received " + String(topic) + ": " + String(data), PRIVATE); // Might cause a bug with storing the name.
    Particle.publish("uName", unitName, PRIVATE);
}

Is this a bug or am I misunderstanding something about the Particle.publish() function?

You cannot use Particle.publish() within a subscription handler without corrupting the data received.
You need to store the data away first.

The reason for this is not a bug but an intentionally shared buffer for publish and subscribe handlers.

1 Like

Thanks, ScruffR. Is there a list somewhere of known conflicts like this with the publish() and subscribe() methods? I know in the docs they some times add warning about stuff like this or add comments in the source code. It seems like publishing the results of a particle subscribe for debugging purposes would be a pretty common situation.

@ZachZ, publishing the results of a subscribe can be done easily by first copying the subscribe response to a char buffer and THEN publishing it as @ScruffR indicated. Nonetheless, there should be a note in the docs warning of the shared buffer.

UPDATE: Just added note in docs :wink:

1 Like