Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a local EasyButton object results in callbacks to random adresses on memory #97

Open
mrroot165 opened this issue Dec 2, 2024 · 0 comments

Comments

@mrroot165
Copy link

I think I found a bug:

Issue
If I create an Easybutton object as a global object, the libary works fine.
However, If I create it as a local object, it misbehaves badly:
The object sometimes performs callbacks to random adresses on the microcontrollers memory, when using EsayButton::read(), causign the programm to malfunction or crash.
This only happens sometimes and only if no callback function was used/specified.

Reason
I think I also found the issue causing this:
In EasyButtonBase.h, the callback function is specified like this:

class EasyButtonBase
{
...
#ifndef EASYBUTTON_DO_NOT_USE_SEQUENCES
    callback_t _pressed_sequence_callbacks[MAX_SEQUENCES];
#endif
    callback_t _pressed_callback;     // Callback function for pressed events.
    callback_t _pressed_for_callback; // Callback function for pressedFor events.

This results in the callbacks being initialized with 0 as the default value if being created as a global object. If created as a local object, they are not initialized and start with a random value.

In EasyButton.cpp it then checks if a callback is set by looking for non-zero values;

	if (_pressed_callback)
	{
		_pressed_callback();
	}

So If ther random starting value is not zero, this results in callbacks to random adresses.

Possible fix

Initialize the values properly:

#ifndef EASYBUTTON_DO_NOT_USE_SEQUENCES
    callback_t _pressed_sequence_callbacks[MAX_SEQUENCES] ={0};
#endif
    callback_t _pressed_callback = 0;     // Callback function for pressed events.
    callback_t _pressed_for_callback = 0; // Callback function for pressedFor events.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant