event handler arguments changed?

Julian
Posts: 2
Joined: Sat Nov 16, 2019 9:41 am

event handler arguments changed?

Postby Julian » Sat Nov 16, 2019 9:56 am

I am writing my code in C++ and I am currently programming a class for WiFi connection. I am trying to use the event_handler_arg to pass the this pointer to my event handler which is a static private function of the class. The relevant setup code looks like this:

Code: Select all

DEBUG_LOG("This: %p", this);
ret = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &WiFiClient::event_handler, this);
if (ret != ESP_OK)
{
	DEBUG_ERR("Unable to register event 1");
	return false;
}

ret = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &WiFiClient::event_handler, this);
if (ret != ESP_OK)
{
	DEBUG_ERR("Unable to register event 2");
	return false;
}
However when I try to access the class it throws an assertion. When I check the pointer it gets changed :shock:
I have the following code in my handler function

Code: Select all

void WiFiClient::event_handler(void* arg, esp_event_base_t event_base, std::int32_t event_id, void* event_data)
{
	[...]
	if (nullptr != event_data)
	{
		DEBUG_LOG("Class ptr: %p", event_data);
		WiFiClient* const client = reinterpret_cast<WiFiClient*>(event_data);
		[...]
This gives the following output:
WifiClient.cpp: This: 0x3ffc83e4
WifiClient.cpp: Class ptr: 0x3ffd55b4

Why is the value changed? The class pointer is obviously not valid anymore.

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: event handler arguments changed?

Postby chegewara » Mon Nov 18, 2019 10:12 am

Is your WiFiClient::event_handler a static function?

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], MicroController and 72 guests