Search code examples
c++openglsdl-2glewsdl-image

"Frame not in module" when using glTexImage2D


I have never come across this error before and I use glTexImage2D elsewhere in the project without error. Below is a screenshot of what error Visual Studio shows, and a view of the disassembly: Frame not in module Disassembly code

Given the line has ptr in it I assume there's a pointer error but I don't know what I'm doing wrong. Below is the function I use to convert from an SDL_surface to a texture.

void surfaceToTexture(SDL_Surface *&surface, GLuint &texture) {
    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
    glDisable(GL_TEXTURE_2D);
}

This function succeeds elsewhere in the program, for example when loading text:

SDL_Surface *surface; 
surface = TTF_RenderText_Blended(tempFont, message.c_str(), color);
if (surface == NULL)
    printf("Unable to generate text surface using font: %s! SDL_ttf Error: %s\n", font.c_str(), TTF_GetError());
else {
    SDL_LockSurface(surface);
    width = surface->w;
    height = surface->h;
    if (style != TTF_STYLE_NORMAL)
        TTF_SetFontStyle(tempFont, TTF_STYLE_NORMAL);
    surfaceToTexture(surface, texture);
    SDL_UnlockSurface(surface);
}
SDL_FreeSurface(surface);

But not when loading an image:

SDL_Surface* surface = IMG_Load(path.c_str());
if (surface == NULL)
    printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
else{
    SDL_LockSurface(surface);
    width = (w==0)?surface->w:w;
    height = (h==0)?surface->h/4:h;
    surfaceToTexture(surface, texture);
    SDL_UnlockSurface(surface);
}
SDL_FreeSurface(surface);

Both examples are extracted from a class where texture is defined. The path to the image is correct. I know it's glTexImage2D that causes the problem as I added a breakpoint at the start of surfaceToTexture and stepped through the function. Even when it doesn't work, texture and surface do have seemingly correct values/properties.

Any ideas?


Solution

  • The error you're getting means, that the procress crashed within a section of code for which the debugger could not find any debugging information (association between assembly and source code) whatsoever. This is typically the case for anything that's part of a/your program's debug build.

    Now in your case what happens is, that you called glTexImage2D with parameters that "lie" to it about the memory layout of the buffer you pointed it to with the data parameter. Pointers don't carry any meaningful meta information (as far as the assembly level is concerned, they're just another integer, with special meaning). So you must make sure, that all the parameters you pass to a function along with a pointer do match up. If not, somewhere deep in the bowles of that function, or whatever it calls (or that calls, etc.) the memory might be accessed in a way that violates constraints set up by the operating system, triggering that kind of crash.

    Solution to your problem: Fix your code, i.e. make sure that what you pass to OpenGL is consistent. It crashes within the OpenGL driver, but only because you lied to it.