0

In my code I create the socket object like so:

UDPSocket sock(&eth);

"eth" is of type EthernetInterface.

All is well for using the "sock" functions inside the function where I created the socket object, however I need to access the same socket to receive UDP packets in a different function.

It can't go with the other global variables because of the function in it, and I have tried setting UDPSocket socket; in globals and then

UDPSocket s(&eth);
sock = s;

This seems to crash the ARM device I am running it on. I don't want to create a UDPObject and then have to pass it as a parameter to every function every time they are called.

1 Answers1

0

Try to make a global pointer like

UDPSocket *socket;

and init it at the beginning:

int main() {
     socket = new UDPSocket(&eth);
     //other code
}

or at least before you're using it. And don't forget to delete it afterwards

Edit: Comment considred using std::unique_ptr<UDPSocket> and to init it with std::make_unique<UDPSocket>(&eth)

philnik
  • 1
  • 2
  • Decent solution, but consider using `std::unique_ptr socket;` and `socket = std::make_unique(&eth);` to nail down ownership and get [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) working for you – user4581301 Nov 08 '18 at 23:43