cocos2d-x关于CC_ASSERT断言BUG!

来源:互联网 发布:php选择题及答案 编辑:程序博客网 时间:2024/04/28 11:01

程序在debug模式下正常运行,而在release模式下却发生莫名的异常,异常非常诡异,弄的我甚至怀疑cocos2d-x的源代码。

晚上在公司比较闲,正好可以慢慢进行排查,最后发现在我自定义的action中出现问题。

这个action负责GLSL特效部分,部分代码如下:

void JEffectAction::startWithTarget(CCNode *pTarget){    CCSprite * spr = dynamic_cast<CCSprite*>(pTarget);        CC_ASSERT(spr);    CCActionInterval::startWithTarget(pTarget);        CCGLProgram* pProgram = new CCGLProgram();    string source = jeffect_source(m_glslfile.c_str());    CC_ASSERT(pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, source.c_str()))    spr->setShaderProgram(pProgram);    pProgram->release();        CHECK_GL_ERROR_DEBUG();    spr->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);    spr->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);    spr->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);    CHECK_GL_ERROR_DEBUG();        spr->getShaderProgram()->link();    CHECK_GL_ERROR_DEBUG();        spr->getShaderProgram()->updateUniforms();    CHECK_GL_ERROR_DEBUG();        // 生成u_time的地址    m_progressUniformLocation = glGetUniformLocation(spr->getShaderProgram()->getProgram(), "u_time");    //    for(map<string,float>::iterator itr = m_uniforms.begin(); itr != m_uniforms.end(); ++itr){        glUniform1f(glGetUniformLocation(pProgram->getProgram(), itr->first.c_str()), itr->second);    }}

通过一行行的排查,最终发现是
CC_ASSERT(pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, source.c_str()))

这一行没有执行,跟进CC_ASSERT代码发现这个宏最终是这样定义的:

#ifdef NDEBUG#defineassert(e)((void)0)#else

也就是说,上面我写的那行代码为空,于是出现异常。

教训就是,在采用assert进行断言时,断言参数不要写逻辑,否则该逻辑在release模式下不能得到执行!


尼玛又要重新审核了。。╮(╯▽╰)╭