0

I have an Android Kotlin application that calls a simple JNI function, the latter calling a C++ singleton. There is a crash (SIGABRT) when I call the JNI function. I can't see what is wrong...

Kotlin code :

stopProcessing() // Calls stopProcessing JNI function

JNI code :

extern "C"
JNIEXPORT void JNICALL
Java_com_tb_of_1ir_MainActivity_stopProcessing(JNIEnv *env, jobject thiz) {
    static auto a = MySingleton::get();
    a->stopProcessing();
}

C++ code :

[[noreturn]] void stopProcessing() {
}

There is nothing in the C++ code for now, but even if something inside (anything) there is the same problem.

Thanks !

Edit :

The singleton code :

#include "MySingleton.h"
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <signal.h>
#include <chrono>
#include <thread>
#include <bitset>

class MySingleton {

private:
    static MySingleton *singleton;
    explicit MySingleton() {
        src_string = "The default string value";
    }

public:
    static MySingleton *get()  {
        if (singleton == nullptr)
            singleton = new MySingleton();
        return singleton;
    }

    bool MySingletonStarted = false;
    bool stop = false;

    std::string dest_string, src_string;

    [[noreturn]] void startProcessing() {
        stop = false;

        MySingletonStarted = true;
        while(!stop) {
            dest_string = src_string;
        }
    }

    void stopProcessing() {
    }

};

MySingleton *MySingleton::singleton = nullptr;
KotlinIsland
  • 799
  • 1
  • 6
  • 25
  • 1
    *I can't see what is wrong...* -- `a->stopProcessing();` -- We have no idea if `a` is a valid pointer. – PaulMcKenzie Jan 18 '21 at 13:33
  • There are a lot of other functions that work. a is valid in all other cases. There is another method named "startProcessing()" and it works. – KotlinIsland Jan 18 '21 at 13:34
  • 1
    `[[noreturn]]`- _"...The __behavior is undefined__ if the function with this attribute actually returns...."_ https://en.cppreference.com/w/cpp/language/attributes/noreturn – Richard Critten Jan 18 '21 at 13:39
  • 1
    Let's see the function `MySingleton::get()`. Also, just because something "works" doesn't mean there isn't an already existing bug. But what else would cause that issue, unless `a` is pointing to somewhere invalid? – PaulMcKenzie Jan 18 '21 at 13:42
  • Here is the singleton code (very basic) : https://pastebin.com/ve2YUrpS – KotlinIsland Jan 18 '21 at 13:50
  • 1
    @KotlinIsland It would be better if you posted that code here. Given that, the singleton is not thread safe. If two or more threads are able to call that `get()` function, then that is definitely an issue. – PaulMcKenzie Jan 18 '21 at 14:09
  • So do you mean I have to make it thread safe ? I've just added the code here in the original question. – KotlinIsland Jan 18 '21 at 14:15
  • 1
    I have not used Kotlin, but definitely, if you can determine that there is a potential for two or more threads to call `get()`, then yes, you must make it thread safe. – PaulMcKenzie Jan 18 '21 at 14:23
  • I sometimes get SIGBUS errors too... Ok, thanks I'll try to make that... – KotlinIsland Jan 18 '21 at 14:24
  • Richard Critten has given the solution indirectly. I've removed the [[noreturn]] statement and everything runs fine now !!! Add an answer and I'll accept it ;) THANKS (to both of you) !!!!!!!! – KotlinIsland Jan 18 '21 at 14:43
  • 1
    @KotlinIsland [See the Meyer's singleton](https://stackoverflow.com/questions/17712001/how-is-meyers-implementation-of-a-singleton-actually-a-singleton). That is thread-safe as per C++ 11. The only issue is that you cannot rely on deterministic destruction of the static instance (shouldn't be a problem if you don't care what happens at the close of a program, and you haven't written code that relies on the destruction of the singleton instance). – PaulMcKenzie Jan 18 '21 at 14:46
  • Yes I've found this link too, but thanks. I've found the solution to my problem, that was because of the [[noreturn]] statement (I added it because of the IDE warnings). I have removed that and now things are fine. I'll check for thread safety too. – KotlinIsland Jan 18 '21 at 14:47

1 Answers1

0

I had to remove the [[noreturn]] statement in the C++ singleton class.

Thanks to Richard Critten who commented (Add an answer and I'll accept it).

KotlinIsland
  • 799
  • 1
  • 6
  • 25