Cocos 2dx项目lua调用OC出现卡死但不闪退的坑
最近新上线的一个游戏,发现线上游戏有部分功能在点击的时候出现了没有反应的情况。通过调试源码,发现是原生OC的代码出现了崩溃,但是比较奇怪的是线上的Bugly没有任何记录,这个功能属于高频高能,而且又是非常重要的功能,所以我下定决心查个明白。后来通过查看cocos2d_lua_bindings的源码,发现是cocos2d_lua_bindings中的CCLuaObjcBridge.mm166行把这个崩溃给catch了。。所以质检部门也没发现这个错误,可能是以为游戏只是卡了下,真的是坑死人不偿命啊。
下面是CCLuaObjcBridge.mm的源码
/**
className
methodName
args
*/
int LuaObjcBridge::callObjcStaticMethod(lua_State *L)
{
if (lua_gettop(L) != || !lua_isstring(L, -) || !lua_isstring(L, -))
{
lua_pushboolean(L, );
lua_pushinteger(L, kLuaBridgeErrorInvalidParameters);
return ;
} const char *className = lua_tostring(L, -);
const char *methodName = lua_tostring(L, -);
if (!className || !methodName)
{
lua_pushboolean(L, );
lua_pushinteger(L, kLuaBridgeErrorInvalidParameters);
return ;
} Class targetClass = NSClassFromString([NSString stringWithCString:className encoding:NSUTF8StringEncoding]);
if (!targetClass)
{
lua_pushboolean(L, );
lua_pushinteger(L, kLuaBridgeErrorClassNotFound);
return ;
} SEL methodSel;
bool hasArguments = lua_istable(L, -);
if (hasArguments)
{
NSString *methodName_ = [NSString stringWithCString:methodName encoding:NSUTF8StringEncoding];
methodName_ = [NSString stringWithFormat:@"%@:", methodName_];
methodSel = NSSelectorFromString(methodName_);
}
else
{
methodSel = NSSelectorFromString([NSString stringWithCString:methodName encoding:NSUTF8StringEncoding]);
}
if (methodSel == (SEL))
{
lua_pushboolean(L, );
lua_pushinteger(L, kLuaBridgeErrorMethodNotFound);
return ;
} NSMethodSignature *methodSig = [targetClass methodSignatureForSelector:(SEL)methodSel];
if (methodSig == nil)
{
lua_pushboolean(L, );
lua_pushinteger(L, kLuaBridgeErrorMethodSignature);
return ;
} @try {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
[invocation setTarget:targetClass];
[invocation setSelector:methodSel];
NSUInteger returnLength = [methodSig methodReturnLength];
const char *returnType = [methodSig methodReturnType]; if (hasArguments)
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
lua_pushnil(L);
while (lua_next(L, -))
{
NSString *key = [NSString stringWithCString:lua_tostring(L, -) encoding:NSUTF8StringEncoding]; switch (lua_type(L, -))
{
case LUA_TNUMBER:
[dict setObject:[NSNumber numberWithFloat:lua_tonumber(L, -)] forKey:key];
break; case LUA_TBOOLEAN:
[dict setObject:[NSNumber numberWithBool:lua_toboolean(L, -)] forKey:key];
break; case LUA_TSTRING:
[dict setObject:[NSString stringWithCString:lua_tostring(L, -) encoding:NSUTF8StringEncoding]
forKey:key];
break; case LUA_TFUNCTION:
int functionId = retainLuaFunction(L, -, NULL);
[dict setObject:[NSNumber numberWithInt:functionId] forKey:key];
break;
} lua_pop(L, );
} [invocation setArgument:&dict atIndex:];
[invocation invoke];
}
else
{
[invocation invoke];
} lua_pushboolean(L, );
if (returnLength > )
{
if (strcmp(returnType, "@") == )
{
id ret;
[invocation getReturnValue:&ret];
pushValue(L, ret);
}
else if (strcmp(returnType, "c") == ) // BOOL
{
char ret;
[invocation getReturnValue:&ret];
lua_pushboolean(L, ret);
}
else if (strcmp(returnType, "i") == ) // int
{
int ret;
[invocation getReturnValue:&ret];
lua_pushinteger(L, ret);
}
else if (strcmp(returnType, "f") == ) // float
{
float ret;
[invocation getReturnValue:&ret];
lua_pushnumber(L, ret);
}
else
{
NSLog(@"not support return type = %s", returnType);
lua_pushnil(L);
}
}
else
{
lua_pushnil(L);
}
return ;
}
@catch (NSException *exception) //就是这处代码导致崩溃被catch了,正确的做法应该是测试阶段直接throw exception,上线后通过bugly手动上报这个exception对象
{ NSLog(@"EXCEPTION THROW: %@", exception); lua_pushboolean(L, ); lua_pushinteger(L, kLuaBridgeErrorExceptionOccurred); return ; } }
Cocos 2dx项目lua调用OC出现卡死但不闪退的坑的更多相关文章
- 苹果手机 微信调用百度地图Javascript API 频繁闪退问题
最近在网页中调用百度地图API js大众版,但是在IOS8系统中,缩放的时候频繁闪退,安卓手机没有这个问题! 在网上查询了下,有网友回答说不要频繁的去new marker,而是初始化话一定量的mark ...
- Cocos 2d-X Lua 游戏添加苹果内购(二) OC和Lua交互代码详解
这是第二篇 Cocos 2d-X Lua 游戏添加苹果内购(一) 图文详解准备流程 这是前面的第一篇,详细的说明了怎样添加内购项目以及填写银行信息提交以及沙盒测试员的添加使用以及需要我们注意的东西,结 ...
- Lua 与 OC 相互调用
本文主要讲如何完成lua和object-c的相互调用. lua是一种脚本语言,可以方便的移植到各种宿主语言中,并且可以支持热更新,在游戏开发中也能当做主要的语言来编写游戏的逻辑,但是要接入 ...
- cocos2d-x Lua与OC互相调用
1. Lua 调用OC 先看例子: hello.lua: -- 点击回调函数 local function notifymenuCallbackTest() local luaoc = require ...
- 在Objc项目中调用Swift
之前的文字中记录了在Swift项目中调用OC的相关代码,比较简单直接 传送门 但是在OC中调用swift代码则不是那么的和谐,网络上很多文章业已经有点陈旧.记录步骤如下: 1.创建OC项目 (1)启动 ...
- Cocos 2d-X Lua 游戏添加苹果内购(一) 图文详解准备流程
事前准备 最近给游戏添加了苹果的内购,这一块的东西也是刚刚做完,总结一下,其实这里不管是游戏还是我们普通的App添加内购这一块的东西都是差不多的,多出来的部分就是我们Lua和OC的交互的部分,以前刚开 ...
- Cocos2d-x下Lua调用自定义C++类和函数的最佳实践[转]
Cocos2d-x下Lua调用C++这事之所以看起来这么复杂.网上所有的文档都没讲清楚,是因为存在5个层面的知识点: 1.在纯C环境下,把C函数注册进Lua环境,理解Lua和C之间可以互相调用的本质 ...
- 【转】Cocos2d-x下Lua调用自定义C++类和函数的最佳实践
转自:http://segmentfault.com/blog/hongliang/1190000000631630 关于cocos2d-x下Lua调用C++的文档看了不少,但没有一篇真正把这事给讲明 ...
- Lua调用自定义C++类
弄了一天终于会Lua调用自定义C++类.不容易啊. 我的电脑是64位的,装了64的Python不行,装了32位的就可以了,靠!下面是报错信息 python pyyaml Cheetah全都是装32位的 ...
随机推荐
- Analyzing the Go runtime scheduler from source code perspective
http://www.cs.columbia.edu/~aho/cs6998/reports/12-12-11_DeshpandeSponslerWeiss_GO.pdf http://www.cs. ...
- Gin框架使用详解
1.什么是Gin Gin是go编写的一个web应用框架. 2.Gin安装 go get github.com/gin-gonic/gin 3.Gin使用示例 package main import ( ...
- XML格式化工具
做接口开发的时候,往往接受参数或返回值是一个XML的字符串.如下图,不方便辨识 两种方法, 1.将它保存为xxx.xml,然后用浏览器打开.这种方法稍微有些麻烦. 2.使用 UltraEdit 工具
- 力导向图Demo
<html> <head> <meta charset="utf-8"> <title>力导向图</title> < ...
- 资源查找器PathMatchingResourcePatternResolver的使用
资源查找器PathMatchingResourcePatternResolver的使用 PathMatchingResourcePatternResolver是一个Ant通配符模式的Resource查 ...
- jenkins GitHub 自动触发
jenkins GitHub 自动触发 转载请注明出处: 转载自Bin's Blog: jenkins GitHub 自动触发( http://www.wenbin.cf/post/54/ ) 需要 ...
- CentOS7搭建以太坊私有链
1. 环境准备:Win10 64位安装 VM VirtualBox,操作系统版本: CentOS-7-x86_64-Everything-1611.iso(7.71G). 切换root账号,方便安装程 ...
- windows下局域网文件共享,不需要登录账号密码
基于局域网中,有时候需要传输一个大的文件都需要用到U盘,很麻烦,所以优选文件共享.但是有时候会出现需要登录账户密码,所以需要设置第三步,步骤如下: 1.选中要共享的文件夹,右键有一个共享选项,点击出现 ...
- MySQL 5.7.14安装说明,解决服务无法启动
http://jingyan.baidu.com/article/f54ae2fc0affca1e92b84999.html http://www.myexception.cn/mysql/51431 ...
- TensorFlow环境 人脸识别 FaceNet 应用(一)验证测试集
TensorFlow环境 人脸识别 FaceNet 应用(一)验证测试集 前提是TensorFlow环境以及相关的依赖环境已经安装,可以正常运行. 一.下载FaceNet源代码工程 git clone ...