Search code examples
c++memory-managementopengl-esandroid-ndknew-operator

crash at (operator new(unsigned int)+22)


im trying to find the cause of the following crash reported on android:

crash at (operator new(unsigned int)+22)

does it mean that memory was not allocated successfully ? and if so, adding std::nothrow and null check and exit the program is the right solution?

is there a way i can limit the program to not allocate in order to reproduce it ?

the code where it crash :

glCompileShader( VSID );

GLint vstat;
glGetShaderiv( VSID, GL_COMPILE_STATUS, &vstat );

if( vstat != GL_TRUE )
{
    GLint infolen;
    glGetShaderiv( VSID, GL_INFO_LOG_LENGTH, &infolen );

    GLchar* infostring = new GLchar[infolen + 1];

    glGetShaderInfoLog( VSID, infolen, nullptr, infostring );
    infostring[infolen] = 0;

    std::stringstream Error;
    Error << "An Error occured while trying to compile"\
        " Vertex Shader \"" << VertexShaderPath
        << "\":\n\n" << infostring;
}

Solution

  • A crash in operator new or new[] can be caused by:

    • unsuccessful allocation. For example if infolen is too large compared to the available memory.
    • a crash in the constructor of the object(s) being allocated. But with GLchar, this is not very probable.

    You should add code to handle the exception, by enclosing new in a try..catch block (as demonstrated here) so to terminate graciously.

    If you invoke new with nothrow you should then check the returned pointer for being different than nullptr in order to avoid nasty UB.