Need help understanding the char buffer of particle publish and subscribe

From the reference

NOTE 2: Particle.publish() and the Particle.subscribe() handler(s) share the same buffer. As such, calling Particle.publish() within a Particle.subscribe() handler will wipe the subscribe buffer! In these cases, copying the subscribe buffer's content to a separate char buffer prior to calling Particle.publish() is recommended.

I think my code has this problem but I don't get what does it mean by copying the subscribe buffer's content to a separate char buffer. The only char buffer i used in my code is the buffer for sprintf.
Can anyone give me an example of how this can be achieved?

A rather simple version would be this

void subHandler(const char* event, const char* data) {
  char buf[strlen(data)+1];  // big enough to hold the string including zero-terminator
  strcpy(buf, data);         // copy data into buffer
  ...
}
1 Like

Thank you! This did fix one of the cause of the crash :slight_smile: