I use ndk to develop android native.

I need pass a class function as callback function, so I use std::function to convert it.

Class Test {
    void suspendStatusCallback(bool isSuspend);
}

I need pass suspendStatusCallback as callback function, so I use the follow code to get the address:

auto f1 = std::function<void(bool)>(std::bind(&Test::suspendStatusCallback, this, std::placeholders::_1));

void (*testFunc)(bool isSuspend) = (void (*)(bool))f1.target<std::__ndk1::__bind<void (Test::*)(bool), Test*, std::__ndk1::placeholders::__ph<1> const&>>();

LOG_ERROR("object is %p, temp test is %p", this, testFunc);

output log is:

01-15 05:01:30.979 15390 15406 I [TouchSensor]: handle:object is 0xb40000746561c500, temp test is 0x74564ddb48

I get a pointer, but when I try to invoke it:

(*testFunc)(true);
or
testFunc(true);

crash occur:

01-15 05:03:21.180 15794 15811 F libc    : Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x787d4b2b48 in tid 15811 (touch-m-Glove), pid 15794 (binder:15794_2)

What is the reason of this crash? What should I do to resolve this problem?

I try to get a not null pointer and success. But this pointer has SEGV_ACCERR error.

New contributor

QueJing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.