I’m in a pointer mess.
I have built a class. On initialisation, an instance of the class binds the address of a function in the main program into a 2D member array.
During certaint points of program execution, elements of this array representing function pointers to functions in the main program are passed into a Static Queue of the class
When the user deletes the instance of the class, the program crashes when servicing the queue, which I think means that queue is pointing to the 2D array in the class instance rather than the original function in the main program. Anyone know how to make this work??
In the class I have:
CLASS HEADER FILE:
func_ptr_t** eventActions = nullptr;
static func_ptr_t m_asyncEventQueue[];
void bind(events event, uint8_t menuLevel, func_ptr_t action);
CLASS SOURCE FILE:
Initialisation of the event action
eventActions = new func_ptr_t*[m_numMenus];
for(int menu = 0; menu < m_numMenus; menu++){
eventActions[menu] = new func_ptr_t[NumEventTypes];
for(int evt = 0; evt < NumEventTypes; evt++){
eventActions[menu][evt] = nullptr;
}
}
Binding of the event actions
void InterruptButton::bind(events event, uint8_t menuLevel, func_ptr_t action){
eventActions[menuLevel][event] = action; // Bind external action to button
}
Adding the action to the static class memember array queue:
void IRAM_ATTR InterruptButton::action(InterruptButton* btn, events event, uint8_t menuLevel){
m_asyncEventQueue[i] = btn->eventActions[menuLevel][event];
}
Later the class instance that has the eventActions array is deleted by the user.
And afterwards when the static queue services tries to call the function in the queue like below:
m_asyncEventQueue[0](); // Action the first entry
The program crashes, so i think it’s referring to the space where the eventActions array was before it was deleted, rather than the original function in main which is shown below:
void menu0Button1keyDown(void) {
Serial.printf("Menu 0, Button 1: Key Down: %lu msn", millis());
}
button1.bind(Event_KeyDown, thisMenuLevel, &menu0Button1keyDown);
Any help would be appreciated, there is just so much pointer stuff going on here with function pointers, arguments, and multi dimensional arrays. Thanks!
Rob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.