I have the following issue which I'm unable to resolve by myself. Perhaps one of you could point me to issues in my code which I don't see.

There is a class called communicator which handles an MQTT connection. I use PubSubClient as a MQTT client. The constructor of communicator looks like the following. Besides connection arguments, it also receives an array of structs SubscriptionHandler. These structs define which topics should be subscribed to and also point to a callback method. When a communicator receives a MQTT message, it compares the topic to the known ones and calls the appropriate callback.

Communicator(char * mqttBrokerIP, uint16_t mqttBrokerPort, char *mqttUsername, char * mqttPassword, char * mqttClientHostname, SubscriptionHandler subscriptionHandlers[], int lenHandlers);

SubscriptionHandler. is defined like this:

typedef void (*callback)();

struct SubscriptionHandler
    char *Topic;
    callback Callback;

I tried the following: In the main file, I defined two methods for demonstration purposes:

void enableHandler()
  Serial.println("enable handler");

void disableHandler()
  Serial.println("disable handler");

The communicator is created in the setup()-Method:

SubscriptionHandler handlers[] = {{&strEnable, enableHandler},
                                    {&strDisable, disableHandler}};

callback (in communicator.h) typedef void (callback)();

strings : String strEnable = ("test/" + hostname + "/command/enable"); and the same for disable. Those are also defined in main.cpp –

As soon as the communicator receives a MQTT message, the ESP32 crashes with the following message:

Guru Meditation Error: Core  1 panic'ed (InstrFetchProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x3ffc9306  PS      : 0x00060c30  A0      : 0x8017e97c  A1      : 0x3ffb1ed0
A2      : 0x3ffc9306  A3      : 0x3ffc9306  A4      : 0x3ffc9306  A5      : 0x3ffc1318
A6      : 0x3ffb1f44  A7      : 0x00000000  A8      : 0x800d4df0  A9      : 0x3ffb1eb0
A10     : 0x0000000d  A11     : 0x0000000b  A12     : 0x3ffc24a4  A13     : 0xa98b32f8
A14     : 0x3ffc1318  A15     : 0xff000000  SAR     : 0x00000004  EXCCAUSE: 0x00000014
EXCVADDR: 0x3ffc9304  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff

Backtrace: 0x3ffc9306:0x3ffb1ed0 0x4017e979:0x3ffb1f00 0x400e280e:0x3ffb1f20 0x400d4f3e:0x3ffb1f70 0x400d5cff:0x3ffb1f90 0x400e4c45:0x3ffb1fb0 0x400889dd:0x3ffb1fd0

Therefore, something is wrong with how I use the function pointer (or somewhere which I don't even think about). Could anyone give me a hint where I could start?

  • 1
    What is callback defined as? And the str... variables - what are they?
    – Majenko
    Nov 2, 2019 at 21:00
  • callback ``` typedef void (callback)();``` *strings** : String strEnable = ("test/" + hostname + "/command/enable"); and the same for disable. Those are also defined in main.cpp
    – pixelgate
    Nov 2, 2019 at 21:07
  • And those strEnable a strDisable strings? If it's char array, you shouldn't have & here and for better readability it should be before function names.
    – KIIV
    Nov 2, 2019 at 21:08
  • You're passing the address of a String object to a char * pointer. That's very bad.
    – Majenko
    Nov 2, 2019 at 21:10
  • have you seen this? randomnerdtutorials.com/…
    – jsotola
    Nov 2, 2019 at 21:12

1 Answer 1


I found the issue. After moving SubscriptionHandler handlers[] = ... out of the setup()-method, the function calls work. Perhaps handlers gets overwritten after leaving the setupfunction scope. Could anyone validate my suspected reason?

  • 1
    Anything in local scope (defined in a function) is gone after the function ends. Even if you store a pointer to it somewhere. It's gone.
    – Majenko
    Nov 2, 2019 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.