Hello,
I have two classes, HR_Display and HR_Control. I need to be able to call methods belonging to HR_Display from inside methods belonging to HR_Control. So I figured that I would pass in a reference to the HR_Display object and store it as a member variable in HR_Control.
That seems to be working okay, assuming I'm doing it correctly. The problem is that when I try to test calling an HR_Display method from HR_Control I get a LoadProhibited crash.
This is HR_Control.h:
#include <Arduino.h>
#include "HR_Display.h"
class HR_Control {
private:
HR_Display _display;
public:
HR_Control(HR_Display& display);
void helloWorld();
};
And the relevant parts of HR_Control.cpp:
#include <Arduino.h>
#include "HR_Display.h"
#include "HR_Control.h"
HR_Control::HR_Control(HR_Display& display) : _display(display)
{
}
void HR_Control::helloWorld() {
_display.helloWorld();
}
I am instantiating them both like so:
HR_Display display(DADDR, DSDA, DSCL);
HR_Control control(display);
And in setup() I am trying to test it:
control.helloWorld();
It is that call to helloWorld() that is causing this crash:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400d41c4 PS : 0x00060530 A0 : 0x800d44a6 A1 : 0x3ffb1cb0
A2 : 0x3ffc0d44 A3 : 0x00000000 A4 : 0x00000000 A5 : 0x3ffb8e18
A6 : 0x00000000 A7 : 0x00000000 A8 : 0x00000000 A9 : 0x00000000
A10 : 0x00000008 A11 : 0x00000000 A12 : 0x0000000c A13 : 0x00000000
A14 : 0x3f400429 A15 : 0x00000000 SAR : 0x00000020 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffc
Backtrace: 0x400d41c4:0x3ffb1cb0 0x400d44a3:0x3ffb1d10 0x400d46bf:0x3ffb1d50 0x400d4ac1:0x3ffb1d80 0x400d356e:0x3ffb1da0 0x400d7fe7:0x3ffb1fb0 0x4008f035:0x3ffb1fd0
Any assistance is greatly appreciated.
Try saving a pointer to the HR_Display, not a copy of it:
private:
HR_Display *_display;
HR_Control::HR_Control(HR_Display& display) : _display(&display) {
}
_display->helloWorld();
Or, join the 21st century and pass a reference, rather than a pointer....
Regards,
Ray L.
RayLivingston:
Or, join the 21st century and pass a reference, rather than a pointer....
I learned ‘C’ in college ~35 years ago (definitely the 20th Century). Only learned C++ when I started w/ Arduino 4 years ago. So, you can consider me an old-time ‘C’ guy.
IMO, Reference were added to C++ in order to do things that you can’t with pointers -- Operator Overloading, Method Chaining, etc. But, when a Good Old-Fashioned ‘C’ pointer will do the job, I use it.
gfvalvo, you are 2 for 2! You sir are a gentlemen and a scholar.
That worked perfectly. Thank you so much!
I guess it's time to really dig in and learn how pointers work.
gfvalvo:
I learned ‘C’ in college ~35 years ago (definitely the 20th Century). Only learned C++ when I started w/ Arduino 4 years ago. So, you can consider me an old-time ‘C’ guy.
IMO, Reference were added to C++ in order to do things that you can’t with pointers -- Operator Overloading, Method Chaining, etc. But, when a Good Old-Fashioned ‘C’ pointer will do the job, I use it.
A reference is nothing more than a pointer in a pretty dress. It has much simpler, more familiar syntax - accessing any data object through a reference is exactly like accessing the data object directly, without having to worry about de-referencing with "*" and "->" - and references do a better job of protecting the user from stupid pointer mistakes that can cause memory leaks, stomping on memory, and other bad things.
Pointers still have their place, but in many/most cases a reference is the better way to go, especially with an inexpert programmer at the keyboard.
Regards,
Ray L.
RayLivingston:
A reference is nothing more than a pointer in a pretty dress. It has much simpler, more familiar syntax - accessing any data object through a reference is exactly like accessing the data object directly, without having to worry about de-referencing with "*" and "->" - and references do a better job of protecting the user from stupid pointer mistakes that can cause memory leaks, stomping on memory, and other bad things.
Pointers still have their place, but in many/most cases a reference is the better way to go, especially with an inexpert programmer at the keyboard.
You're welcome to have your own preferences.