死机问题排查terminate called after throwing an instance of ‘std::logic_error’
what(): basic_string::_M_construct null not valid
问题简介
来源百度:
报错解释:
std::logic_error 是 C++ 标准库中定义的一种异常,用于指示程序中存在逻辑错误。在这个错误中,basic_string::_M_construct null no 表明尝试构造一个 std::string 对象时传入了空指针,并且这通常意味着程序试图使用空指针构造字符串。
解决方法:
检查传递给 std::string 构造函数或赋值操作的指针是否为空。确保不是传入了 nullptr 或者 0。
如果是从函数返回 std::string,确保不要返回指向局部变量的指针或引用。
如果是从外部源(如文件或网络)接收数据,确保在尝试构造 std::string 之前检查数据是否有效。
问题排查
static const char* g_ls_sound[64] = {
EDOG_RES_VOICE_LS_MP3(LS_0),
EDOG_RES_VOICE_LS_MP3(LS_1),
EDOG_RES_VOICE_LS_MP3(LS_2),
EDOG_RES_VOICE_LS_MP3(LS_3),
EDOG_RES_VOICE_LS_MP3(LS_4),
EDOG_RES_VOICE_LS_MP3(LS_5),
EDOG_RES_VOICE_LS_MP3(LS_6),
EDOG_RES_VOICE_LS_MP3(LS_7),
EDOG_RES_VOICE_LS_MP3(LS_8),
EDOG_RES_VOICE_LS_MP3(LS_9),
EDOG_RES_VOICE_LS_MP3(LS_10),
};
该数组实际可访问为0-10,但代码中引用该数组时实际有11,引起数组越界。
问题解决
static const char* g_ls_sound[64] = {
EDOG_RES_VOICE_LS_MP3(LS_0),
EDOG_RES_VOICE_LS_MP3(LS_1),
EDOG_RES_VOICE_LS_MP3(LS_2),
EDOG_RES_VOICE_LS_MP3(LS_3),
EDOG_RES_VOICE_LS_MP3(LS_4),
EDOG_RES_VOICE_LS_MP3(LS_5),
EDOG_RES_VOICE_LS_MP3(LS_6),
EDOG_RES_VOICE_LS_MP3(LS_7),
EDOG_RES_VOICE_LS_MP3(LS_8),
EDOG_RES_VOICE_LS_MP3(LS_9),
EDOG_RES_VOICE_LS_MP3(LS_10),
EDOG_RES_VOICE_LS_MP3(LS_11),
EDOG_RES_VOICE_LS_MP3(LS_12),
EDOG_RES_VOICE_LS_MP3(LS_13),
EDOG_RES_VOICE_LS_MP3(LS_14),
EDOG_RES_VOICE_LS_MP3(LS_15),
EDOG_RES_VOICE_LS_MP3(LS_16),
EDOG_RES_VOICE_LS_MP3(LS_17),
EDOG_RES_VOICE_LS_MP3(LS_18),
};
完善该数组的定义即可