1 问题背景
某一次写好代码运行起来,崩溃了。具体如下:
12-27 11:45:13.143 10688 10688 I crash_dump64: performing dump of process 10631 (target tid = 10665)
12-27 11:45:13.145 10688 10688 F DEBUG : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
12-27 11:45:13.145 10688 10688 F DEBUG : Build fingerprint: 'Xiaomi/sagit/sagit:8.0.0/OPR1.170623.027/V10.2.1.0.OCACNFK:user/release-keys'
12-27 11:45:13.145 10688 10688 F DEBUG : Revision: '0'
12-27 11:45:13.145 10688 10688 F DEBUG : ABI: 'arm64'
12-27 11:45:13.145 10688 10688 F DEBUG : pid: 10631, tid: 10665, name: GLThread 7409 >>> com.chenxf.opengles <<<
12-27 11:45:13.145 10688 10688 F DEBUG : signal 5 (SIGTRAP), code 1 (TRAP_BRKPT), fault addr 0x718ce59eb8
12-27 11:45:13.145 10688 10688 F DEBUG : x0 0000000000000030 x1 000000718a5b0730 x2 0000000000000005 x3 0000000000000003
12-27 11:45:13.145 10688 10688 F DEBUG : x4 003d3d3d3d3d204a x5 0080008000000080 x6 26204f3d44495400 x7 80544944bd4fa026
12-27 11:45:13.145 10688 10688 F DEBUG : x8 484555970ac3469f x9 484555970ac3469f x10 000000718a5b08c0 x11 0000000000000026
12-27 11:45:13.145 10688 10688 F DEBUG : x12 000000000000000b x13 0000000000000001 x14 ffffffffffffffff x15 26204f3d44495400
12-27 11:45:13.145 10688 10688 F DEBUG : x16 00000071a87e7cd0 x17 00000071aa458b1c x18 0000000000000003 x19 00000071987a8400
12-27 11:45:13.145 10688 10688 F DEBUG : x20 000000718ce2a5cc x21 00000071987a8400 x22 000000718a5b156c x23 00000071a8dc0c0f
12-27 11:45:13.145 10688 10688 F DEBUG : x24 0000000000000004 x25 00000071987a8498 x26 0000000000000000 x27 0000000000000000
12-27 11:45:13.145 10688 10688 F DEBUG : x28 0000000000000001 x29 000000718a5b0f40 x30 000000718ce59eb4
12-27 11:45:13.145 10688 10688 F DEBUG : sp 000000718a5b0e20 pc 000000718ce59eb8 pstate 0000000060000000
12-27 11:45:13.147 10688 10688 F DEBUG :
12-27 11:45:13.147 10688 10688 F DEBUG : backtrace:
12-27 11:45:13.147 10688 10688 F DEBUG : #00 pc 0000000000052eb8 /data/app/com.chenxf.opengles-lsgMCmdjPbuhdERQwZJhbg==/base.apk (offset 0x3675000)
12-27 11:45:13.147 10688 10688 F DEBUG : #01 pc 00000000000529cc /data/app/com.chenxf.opengles-lsgMCmdjPbuhdERQwZJhbg==/base.apk (offset 0x3675000)
也没有具体的so堆栈信息,唯一线索,就是
signal 5 (SIGTRAP), code 1 (TRAP_BRKPT)
以及代码的最后执行的日志打印。
抽取代码如下:
void TextSample::Init() {
//......省略
initFreetype();
}
int TextSample::initFreeType() {
FT_Library ft;
// All functions return a value different than 0 whenever an error occurred
if (FT_Init_FreeType(&ft)) {
LOGCATE("ERROR::FREETYPE: Could not init FreeType Library");
return -1;
}
FT_Face face;
if (FT_New_Face(ft, font_name.c_str(), 0, &face)) {
LOGCATE("ERROR::FREETYPE: Failed to load fonts");
return -1;
} else {
//......省略
}
// destroy FreeType once we're finished
FT_Done_Face(face);
FT_Done_FreeType(ft);
DEBUG_LOGCATE("initFreeType done ");
}
日志最后打印了initFreeType done
2 原因和解决办法
一开始以为,initFreeType done
都打印了,跟这个函数没关系,其实,有很大的关系。
SIGTRAP
的错误,指的是函数没有返回值!
所以写C++代码,要小心,虽然没返回值能编译过,但是最终会运行崩溃。
解决办法就是,如果一个函数定义了有返回值,那要保证各种case下,都能有返回值,即都有return
。