从java调用本机方法时的Libc SIGSEGV

时间:2012-12-05 11:17:04

标签: android c++ java-native-interface amazon native-methods

我正在尝试让我的Amazon In-app Purchases SDK的PurchasingObserver的java实现通过本机方法与我的C ++代码进行通信。

PurchasingObserver.java //摘录

public class PurchasingObserver
{
    //...

    private native void postEvent(int type, String jsonData);
    // called by each of the four event handler methods, data is non-null
    // also tried it as native synchronized
}

AmazonInAppPurchaseHandler.cpp //摘录

static AmazonInAppPurchaseHandler* AmazonInAppPurchaseHandler::s_pInstance(0); // dumbleton

JNIEXPORT void JNICALL  _NativePurchasingObserverPostEvent(JNIEnv* pEnv, jobject obj, jint type, jstring jsonData)
// friend function
{
  assert(type >= 0);
  assert(type < AmazonInAppPurchaseHandler::kNumEventTypes); // event type is in range
  assert(pEnv != 0); // JNI environment is valid

  printf("Stuff from the native callback: %d, %p\n", type, jsonData); // never gets printed.

  int jsonDataLen(0);
  const char* pJsonDataUtfChars(0);
  if(jsonData != 0)
  {
    jsonDataLen = pEnv->GetStringUTFLength(jsonData);
    pJsonDataUtfChars = pEnv->GetStringUTFChars(jsonData, 0);
  }

  assert(s_pInstance != 0); // got AmazonInAppPurchaseHandler instance
  s_pInstance->DoCallback(type, jsonDataLen, pJsonDataUtfChars);

  pEnv->ReleaseStringUTFChars(jsonData, pJsonDataUtfChars);
}

static const JNINativeMethod karNativeMethod[] =
{
  { 
    "postEvent",
    "(ILjava/lang/String;)V",
    (void*)&_NativePurchasingObserverPostEvent
  }
};


AmazonInAppPurchaseHandler::AmazonInAppPurchaseHandler()
{
  assert(s_pInstance == 0); // is only instance
  s_pInstance = this;

  JNIEnv* pEnv(GetJNIEnv());
  assert(pEnv != 0); // got JNI environment

  jint  result(pEnv->RegisterNatives(cPurchaseObserver, karNativeMethod, 1));
  assert(AmazonInAppPurchaseHandler, result == 0); // successfully registered
}

AmazonInAppPurchaseHandler::~AmazonInAppPurchaseHandler()
{
  s_pInstance = 0;
}

只要我做任何产生事件的事情,就会发生以下情况: 1,正确调用事件处理程序,它执行日志记录。 2,它还记录“Posting event ...”,这是调用postEvent()之前的最后一件事。 3,程序在libc中与SIGSEGV崩溃。 (它指的是我的申请,但请注意反向网络名称被截断。我不确定这是不寻常的。) 4,永远不会到达C ++ _NativePurchasingObserverPostEvent中的跟踪。

日志:

  

12-05 10:24:47.380:   D/com.mycompany.amazoninapp.PurchasingObserver@41970368(4604):   onGetUserIdResponse:   (com.amazon.inapp.purchasing.GetUserIdResponse@4196cf98,inquestId:   “dcf8e712-078b-4d47-9533-ee9ae544f53d”,getUserIdRequestStatus:   “SUCCESSFUL”,userId:“DefaultTestUser”)

     

12-05 10:24:47.380:   D/com.mycompany.amazoninapp.PurchasingObserver@41970368(4604):   发布活动......

     

12-05 10:24:47.380:A / libc(4604):致命信号11(SIGSEGV)at   0x00000008(代码= 1),线程4604(y.amazoninapp)

     

12-05 10:24:47.390:I / AmazonSDKTester(3529):发送购买更新   响应广播   ({ “revokedSkus”:[], “偏移量”: “1354703087397”, “状态”: “成功”, “的requestId”: “b9aee42e-4f50-42c4-8a12-ba9eb1d19155”, “isMore”:假 “收据” :[{ “SKU”: “com.mycompany.amazoninapp.ENTI01”, “标记”: “eyJ0eXBlIjoiTk9OQ09OU1VNQUJMRSIsInNrdSI6ImNvbS5wbGF5ZXJ0aHJlZS5hbWF6b25pbmFw \ ncC5FTlRJMDEifQ \ n”, “项目类型”: “题为”}], “用户id”: “DefaultTestUser”})

我经历了正确获取JNI类名和方法签名字符串的迭代(我们已经过了UnsatisfiedLinkErrors);我正在仔细检查null字符串。我的其他JNI工作正常,亚马逊应用程序内购买UI正确显示。保证在注册本机方法之前不会生成事件。

造成撞车的原因是什么?

感谢您提前输入。

2 个答案:

答案 0 :(得分:0)

您需要将您的函数声明为使用C调用约定。将其声明为extern "C",如下所示:

extern "C" void _NativePurchasingObserverPostEvent(JNIEnv* pEnv, jobject obj, jint type, jstring jsonData)

答案 1 :(得分:0)

尝试使用javah在{+ 1}}中创建一个标题,而不是自己编写函数原型。