当我调用NPN_GetValue时,我的插件代码崩溃了。基本上我创建了一个脚本化对象,它有一个'getDevice'方法,可以将设备数组返回给JavaScript。以下是代码段。
static bool mainNPObjectInvoke(NPObject *obj, NPIdentifier identifier, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
printf("create main object");
MainNPObject *mainObject = (MainNPObject *)obj;
if (identifier == methodIdentifiers[METHOD_ID_GET_DEVICES])
{
NPObject *windowObj = NULL;
browser->getvalue(mainObject->npp, NPNVWindowNPObject, &windowObj);
// it crashed here
....
}
}
我使用以下方法创建了MainNPObject实例。
NPObject *createMainNPObject(NPP npp)
{
MainNPObject *object = (MainNPObject *)browser->createobject(npp, &mainNPClass);
object->npp = npp;
theMainObject = object;
return (NPObject *)object;
}
在我提供给浏览器的插件函数中调用createMainNPObject。
NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
{
PluginObject *obj = instance->pdata;
switch (variable) {
case NPPVpluginCoreAnimationLayer:
if (!obj->rootLayer)
setupLayerHierarchy(obj);
*(CALayer **)value = obj->rootLayer;
return NPERR_NO_ERROR;
case NPPVpluginScriptableNPObject:
if (!obj->mainObject)
{
obj->mainObject = createMainNPObject(instance);
}
....
}
分配功能如下。
static NPObject *mainNPObjectAllocate(NPP npp, NPClass *class)
{
initializeIdentifiers();
MainNPObject *mainObject = malloc(sizeof(MainNPObject));
mainObject->deviceManager = [[DeviceManager alloc] init];
return (NPObject *)mainObject;
}
MainNPObject的定义:
typedef struct
{
NPObject *npobject;
NPP npp;
DeviceManager *deviceManager;
} MainNPObject;
通过调试代码,我发现系统在调用browser-> getValue时引发了EXC_BAD_ACCESS,看起来npp指针无效。
0x00007fff83f82dab <+0019> je 0x7fff83f82db9 <_ZN6WebKit14NetscapePlugin7fromNPPEP4_NPP+33>
0x00007fff83f82dad <+0021> incl 0x8(%rax)
有人能帮助我吗?
谢谢!
答案 0 :(得分:0)
嗯;看不出任何明显的东西尝试将另一个参数(一个int?)添加到您的结构中并在分配期间或之后立即设置它,然后检查它是否仍然是您在调用getvalue之前设置的值。看看你的结构是否以某种方式变得腐败。当我以非显而易见的方式投射NPObject时,这种情况发生在我身上。