-
Notifications
You must be signed in to change notification settings - Fork 13.3k
memory leak in functionalInterrupt #4817
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
Comments
Actually, I don't understand why there isn't a |
@bertmelis this is a known issue, with a fix already available, but untested. Please test pr #4609 , and report back here. |
Thanks for the feedback. I'll post the results. What do you think about making the attachInterrupt-with-argument available? Everything is already in the code. Or is it not compatible with FunctionalInterrupt? |
I suspect it's not needed. AttachInterrupt() takes as arg a std::function, which means you can pass in a lambda that references context variables, or a functor that saves a context internally. Off the top of my head, I think one of those would accomplish the same. |
😄 I know, but I just want to pass a class method to the interrupt. A std::function is somewhat overkill. Don't get me wrong: I want both funtional and argument be possible. |
@bertmelis You can use std::bind to use a class function. This is one line from my code |
I know. That's also how I do it now. But after examining the interrupt code I wondered why the argument is not available. Then I could do this: attachInterrupt(reqEchoPin, (cast)&Class::handler, this, CHANGE );
// and
void Class::handler(Class* instance) {
} This avoids the overhead of functional/bind and saves some memory. The only reasons I can come up with is that "standard/original" Arduino doesn't have this or that it is incompatible with the code to make functionalInterrupt working. |
I am not 100% sure but I think that "standard/original" arduino does not have the option for argument. I do not understand your example. How would you get the function from |
Since the implementation would still need to support attaching an arbitrary function object, there would likely be no actual memory savings. An implementation tuned specifically for your case would only save one word (4 bytes) compared to the one based on std::function (using 12 vs 16 bytes). With regards to code overhead, i don't understand where it would come from. There's one call to via vtable to the template implementing the call to your class method, and then one call from there to the class method. Hand-written implementation would do exactly the same... |
Anyways, using the code from the PR, the memory leak is gone. So I'm good to go the way it was (heading at). PS: I updated my example from my earlier post. |
@igrr You're right. I was thinking of having both the functionpointer+argument method and std::function method available. But I lost the fact that you cannot overload a function pointer with a std::function. |
Memleak solved with PR #4609 |
memory leak on functional interrupt
attachInterrupt()
call.https://github.com/esp8266/Arduino/blob/master/cores/esp8266/FunctionalInterrupt.cpp#L20
Every time you call
attachInterrupt()
a new ArgStructure gets created and doesn't get destroyed.When I create a (global) ArgStructure and just change the std::function inside the struct (hence reusing the ArgStructure), the memory leak disappears. Obviously, this isn't a valid solution.
The text was updated successfully, but these errors were encountered: