sugelawa 发表于 2014-04-18 18:25

为什么在使用Android native Thread时报错Fatal signal 11 (SIGSEGV)?

我仿效Android的代码写了这样一个非常简单的Android C++ Thread类使用的测试程序:先构建一个WorkerInterface基类,然后派生了一个Worker类,他们有一个loopOnce方法。另外从Thread派生了一个WorkerThread类,用来实现多线程,WorkerThread有一个sp<WorkerInterface> mWorker的成员变量,它在WorkerThread构造函数中初始化 mWorker = new Worker()。在WorkerThread的threadLoop方法中调用成员变量mWorker的loopOnce方法,但在这个地方报错Fatal signal 11 (SIGSEGV)。整了几点怎么试都不能解决,有大侠能帮忙诊断下吗,我将无比感激。#include <jni.h>
#include <cutils/log.h>
#include <utils/threads.h>
#include <utils/RefBase.h>

namespace android {


//------------------------------------------------
class WorkerInterface : public virtual RefBase {
protected:
    WorkerInterface() { }
    virtual ~WorkerInterface() { }

public:
    virtual void loopOnce() = 0;
};
//------------------------------------------------


//------------------------------------------------
// Worker class which will do the job in the Thread
class Worker : public WorkerInterface {
public:
        Worker();
    virtual ~Worker();
   
    virtual void loopOnce();
};


// Implement of the Worker class
Worker::Worker() {
}

Worker::~Worker() {
}

void Worker::loopOnce() {
    ALOGE("+++++++++++++++++++++++++++++Worker::loopOnce");
}
//------------------------------------------------

//------------------------------------------------
// Worker thread class which construct the thread
class WorkerThread: public Thread {
public:
        WorkerThread();
        virtual bool threadLoop();
private:
        sp<WorkerInterface> mWorker;
};


// Implement of the WorkerThread class
WorkerThread::WorkerThread() : Thread(false) {
        mWorker = new Worker();
}

bool WorkerThread::threadLoop() {
        ALOGD("------------------threadLoop");
      // *******************======>下面就是罪恶的报错的地方: Fatal signal 11 (SIGSEGV)
        mWorker->loopOnce();   
        return true;
}
//------------------------------------------------

//------------------------------------------------
JNIEXPORT jint JNICALL Java_me_autotouch_autotouch_Kernel_nativeReadyToRecord(JNIEnv *env, jobject obj) {
       
        sp<WorkerThread> thread = new WorkerThread();
        thread->run("WorkerThread", PRIORITY_URGENT_DISPLAY);
        while(1);
    ALOGD("jni calling end");

        return 0;
}

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
        return JNI_VERSION_1_4;
}
//------------------------------------------------

sugelawa 发表于 2014-04-24 22:20

顶上去顶上去顶上去
页: [1]
查看完整版本: 为什么在使用Android native Thread时报错Fatal signal 11 (SIGSEGV)?