Avoid freeing memory in detachInterrupt #2356
Open
+76
−27
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When
attachHandler
is called with a function pointer (or a pointer to a member function and instance), it allocates astd::function
. WhendetachHandler
is then called on the same pin, it callsdelete
to free that memory. That causes a heap error if you try to calldetachHandler
on this pin from an ISR. The same is true for attaching and detaching system interrupts.Solution
This PR removes the
delete
s and leaves the memory allocated to be re-used the next timeattach*
is called. This prevents the application from reclaiming the memorystd::function
is allocating on the heap, but I suspect that's OK since there can be no more allocations than the number of pins.I think the alternative is to update the documentation to specify when
detachInterrupt
can be called. It's probably still worth updating the docs to specify thatattachInterrupt
andattachSystemInterrupt
cannot be called from an ISR when used with a function pointer / class+member.Steps to Test
Wrote and ran an integration test on my Photon.
Example App
Deferring to the integration test, but can write something if necessary.
References
The
delete
indetachSystemInterrupt
was added in #951. I think the primary problem fixed in that PR was an unbounded memory leak repeatedly callingattachSystemInterrupt
.Completeness