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

lambda function modifying member variables segfaults as isr callback #985

Open
AndreasAZiegler opened this issue Oct 2, 2019 · 0 comments

Comments

@AndreasAZiegler
Copy link

I want to use a lambda function as a callback function form my isr in the following way.

class MyClass {
public:
  void initialize() {
    m_local_variable = 10;
    auto isr_callback = [&m_local_variable = m_local_variable]() {
    m_local_variable = 15;
    std::cout << m_local_variable << std::endl;
  };
  auto isr_thunk = [](void *args) { (*static_cast<decltype(isr_callback) *>(arg))(); };
  status  = m_gpio_safety_A->isr(mraa::EDGE_BOTH, isr_thunk, &isr_callback);
  }
private:
  int m_local_variable;
};

On an interrupt I get the following stack trace.

Thread 127 "mt-safety-field" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fff1a61c700 (LWP 22171)]
SafetyFieldIntrusionDetector::<lambda()>::operator()(void) const (__closure=0x7fffffffc0e8)
    at /home/mtr/ros/safety_field_intrusion_detection_ws/src/safety_field_detector/src/SafetyFieldIntrusionDetector.cpp:54
54	    m_local_variable = 15;
(gdb) bt full
#0  SafetyFieldIntrusionDetector::<lambda()>::operator()(void) const (__closure=0x7fffffffc0e8)
    at /home/mtr/ros/safety_field_intrusion_detection_ws/src/safety_field_detector/src/SafetyFieldIntrusionDetector.cpp:54
        m_local_variable = <error reading variable>
#1  0x000055555563ed76 in SafetyFieldIntrusionDetector::<lambda(void*)>::operator()(void *) const (
    __closure=0x0, arg=0x7fffffffc0e8)
    at /home/mtr/ros/safety_field_intrusion_detection_ws/src/safety_field_detector/src/SafetyFieldIntrusionDetector.cpp:58
No locals.
#2  0x000055555563ed96 in SafetyFieldIntrusionDetector::<lambda(void*)>::_FUN(void *) ()
    at /home/mtr/ros/safety_field_intrusion_detection_ws/src/safety_field_detector/src/SafetyFieldIntrusionDetector.cpp:58
No locals.
#3  0x00007ffff7993a35 in mraa_gpio_interrupt_handler () from /usr/local/lib/libmraa.so.2
No symbol table info available.
#4  0x00007ffff56ae6db in start_thread (arg=0x7fff1a61c700) at pthread_create.c:463
        pd = 0x7fff1a61c700
        now = <optimized out>
        unwind_buf = {cancel_jmp_buf = {{jmp_buf = {140733636003584, -3791838349273916627, 
                140733635996928, 0, 93825086380816, 140737488338896, 3791639322018650925, 
                3791826142144961325}, mask_was_saved = 0}}, priv = {pad = {0x0, 0x0, 0x0, 0x0}, 
            data = {prev = 0x0, cleanup = 0x0, canceltype = 0}}}
        not_first_call = <optimized out>
#5  0x00007ffff31bf88f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
No locals.

Is there any other way how to use a lambda function as isr callback?

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