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

Free memory allocated for previous system interrupt handler #951

Merged
merged 2 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion hal/src/stm32/system_interrupts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ uint8_t HAL_Set_System_Interrupt_Handler(hal_irq_t irq, const HAL_InterruptCallb
*previous = cb;
if (callback)
cb = *callback;
else
else {
cb.handler = 0;
cb.data = 0;
}

return true;
}
Expand Down
43 changes: 43 additions & 0 deletions user/tests/wiring/no_fixture/interrupts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,46 @@ test(interrupts_atomic_section)

assertMore(end_millis, start_millis);
}

namespace
{

class TestHandler
{
public:
TestHandler()
{
++count;
}

TestHandler(const TestHandler&)
{
++count;
}

~TestHandler()
{
--count;
}

void operator()()
{
}

static int count;
};

} // namespace

int TestHandler::count = 0;

test(interrupts_detached_handler_is_destroyed)
{
assertEqual(TestHandler::count, 0);
attachSystemInterrupt(SysInterrupt_SysTick, TestHandler());
assertEqual(TestHandler::count, 1);
attachSystemInterrupt(SysInterrupt_SysTick, TestHandler()); // Override current handler
assertEqual(TestHandler::count, 1); // Previous handler has been destroyed
detachSystemInterrupt(SysInterrupt_SysTick);
assertEqual(TestHandler::count, 0);
}
10 changes: 8 additions & 2 deletions wiring/src/spark_wiring_interrupts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ bool attachSystemInterrupt(hal_irq_t irq, wiring_interrupt_handler_t handler)
callback.handler = call_wiring_interrupt_handler;
wiring_interrupt_handler_t& h = handler;
callback.data = new wiring_interrupt_handler_t(h);
return HAL_Set_System_Interrupt_Handler(irq, &callback, NULL, NULL);
HAL_InterruptCallback prev = { 0 };
const bool ok = HAL_Set_System_Interrupt_Handler(irq, &callback, &prev, NULL);
delete (wiring_interrupt_handler_t*)prev.data;
return ok;
}

/**
Expand All @@ -160,5 +163,8 @@ bool attachSystemInterrupt(hal_irq_t irq, wiring_interrupt_handler_t handler)
*/
bool detachSystemInterrupt(hal_irq_t irq)
{
return HAL_Set_System_Interrupt_Handler(irq, NULL, NULL, NULL);
HAL_InterruptCallback prev = { 0 };
const bool ok = HAL_Set_System_Interrupt_Handler(irq, NULL, &prev, NULL);
delete (wiring_interrupt_handler_t*)prev.data;
return ok;
}