EXC_BAD_ACCESS with gluBuild2DMipmaps

Post: #1
NOTE: I believe the problem to be fixed now. See the next post for an explanation.

Hi guys,

I'm choosing to post this here, on the grounds that the error happens during the gluBuild2DMipmaps call, although I doubt it's actually overly related to OpenGL.

A few weeks (months?) ago, somebody gave me their texture loading class, which I altered slightly (largely just so that it uses mipmaps) and have used a couple of times since. However, in my latest project, I'm quite clearly doing something very wrong, as I get the following two occurrances when using this class:

1. When I Build and Run the program, it attempts to open, and then the debugger opens, displaying an EXC_BAD_ACCESS error, which seems to be somewhere in the middle of the gluBuild2DMipmaps() call.

2. If I place an NSLog() a few lines above this (I originally did this just to check a couple of values without using the debugger), the program Builds and Runs fine, and no error occurs. I don't understand this at all, and it leads me to believe that I'm doing something very wrong, somewhere along the way, but can't seem to figure out where.

Here's the texture loading method, and below it is a link to a screenshot of the debugger window when the program crashes.

- (id)initWithImageAtPath:(NSString*)filePath {
    self = [super init];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSLog(@"Can't find image %@", [filePath lastPathComponent]);
        return 0;
    CFURLRef texture_url = CFURLCreateWithFileSystemPath (   NULL,
    CGImageSourceRef image_source = CGImageSourceCreateWithURL( texture_url,
    assert(CGImageSourceGetCount(image_source) > 0);
    CGImageRef image = CGImageSourceCreateImageAtIndex(image_source,
    width = CGImageGetWidth(image);
    height = CGImageGetHeight(image);
    void *data = malloc(width * height * 4);
    CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(data,
                                             width * 4,
                       CGRectMake(0, 0, width, height),
    vImage_Buffer normal, flipped;
    normal.data = data; // The data buffer you used for creating the CGContextRef
    normal.height = height;
    normal.width = width;
    normal.rowBytes = width * 4;
    flipped.data = malloc(4 * height * width);
    flipped.height = height;
    flipped.width = width;
    flipped.rowBytes = width * 4;
    vImage_Error error = vImageVerticalReflect_ARGB8888(&normal, &flipped, 0);
    if(error != kvImageNoError)
        assert(error != kvImageNoError);
    data = flipped.data;
    /******       NSLog(@"If I put an NSLog here, then the program builds and runs fine.");      ********/
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    GLfloat fLargest;
    glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &fLargest);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
    return self;


If you need any more information or code snippets, don't hesitate to ask.

Thanks in advance,
Quote this message in a reply
Post: #2
EDIT: But wait, I was wrong here too! Keep scrolling down to find out the real reason.

Okay, upon advice from #idevgames and OneSadCookie, I swapped the gluBuild2DMipmaps for a glTexImage2D, and the problem went away. OneSadCookie informs me gluBuild2DMipmaps are "notoriously flaky" and that I should use SGIS_generate_mipmaps instead, so I shall attempt to do that.
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #3
That said, I thought gluBuild2DMipmaps had been fixed since 10.4, at least. If it's really the cause of the problem (as opposed to some other random memory trasher elsewhere in the code), then it deserves a bug filed.
Quote this message in a reply
Post: #4
Okay, as it turns out I was trying to load my textures too early (I was calling my load texture method in -awakeFromNib).
I have since shifted the call to -prepareOpenGL, and everything works smoothly now.

Apologies for the time I've wasted with this one,
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Segfault in gluBuild2DMipmaps() IBethune 6 7,010 Feb 1, 2007 10:41 AM
Last Post: arekkusu
  how to use Glubuild2DMipmaps, GlTexImage2D and glTexParameter 007gamer 1 6,112 Jan 31, 2007 04:36 AM
Last Post: OneSadCookie
  OpenAL, Ogg/Vorbis music, and EXC_BAD_ACCESS 0ctane 10 13,076 May 28, 2006 01:43 PM
Last Post: OneSadCookie
  open AL crash with EXC_BAD_ACCESS carlo 9 7,414 Apr 12, 2006 09:23 PM
Last Post: Sabrina
  Stumped by EXC_BAD_ACCESS maaaaark 8 6,685 Mar 24, 2005 11:19 PM
Last Post: arekkusu