xcode memory error thing issue

Member
Posts: 194
Joined: 2009.02
Post: #1
I'm getting this error in my console after running my project in xcode:

cocoa iso engine(21879,0xa083e720) malloc: *** mmap(size=839909376) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

I'm aware this has something to do with a memory leak or perhaps not releasing memory. I use libpng for loading pngs for my opengl textures and I've managed to trace this error back to the png_texture routine, here is the routine itself plus load_png which it calls:

Code:
void png_texture(texture_type* texture, char *filename)
{
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    
    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    
    int w, h;
    GLubyte *pixels = (GLubyte *)load_png(filename, &w, &h);
    
    
    texture->width  = w;
    texture->height = h;
    texture->texID = tex;
    
    GLuint img_size = w * h * GL_RGBA/8;
    texture->imageData = (GLubyte *)malloc(img_size);
    texture->imageData = pixels;
    texture->bpp = GL_RGBA;
    
    
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    
    
    *pixels = nil;
    //free(pixels);
}


...... and here's load_png......




Code:
char *load_png(char *name, int *width, int *height)
{
    FILE *png_file = fopen(name, "rb");
    assert(png_file);

    uint8_t header[PNG_SIG_BYTES];

    fread(header, 1, PNG_SIG_BYTES, png_file);
    assert(!png_sig_cmp(header, 0, PNG_SIG_BYTES));

    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    assert(png_ptr);

    png_infop info_ptr = png_create_info_struct(png_ptr);
    assert(info_ptr);

    png_infop end_info = png_create_info_struct(png_ptr);
    assert(end_info);

    assert(!setjmp(png_jmpbuf(png_ptr)));
    png_init_io(png_ptr, png_file);
    png_set_sig_bytes(png_ptr, PNG_SIG_BYTES);
    png_read_info(png_ptr, info_ptr);

    *width = png_get_image_width(png_ptr, info_ptr);
    *height = png_get_image_height(png_ptr, info_ptr);

    png_uint_32 bit_depth, color_type;
    bit_depth = png_get_bit_depth(png_ptr, info_ptr);
    color_type = png_get_color_type(png_ptr, info_ptr);
            
    if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
        png_set_gray_1_2_4_to_8(png_ptr);

    if (bit_depth == 16)
            png_set_strip_16(png_ptr);
            
    if(color_type == PNG_COLOR_TYPE_PALETTE)
        png_set_palette_to_rgb(png_ptr);
    else if(color_type == PNG_COLOR_TYPE_GRAY ||
            color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
        {
            png_set_gray_to_rgb(png_ptr);
        }

    if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
        png_set_tRNS_to_alpha(png_ptr);
    else
        png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);

    png_read_update_info(png_ptr, info_ptr);

    png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
    png_uint_32 numbytes = rowbytes*(*height);
    png_byte* pixels = malloc(numbytes);
    png_byte** row_ptrs = malloc((*height) * sizeof(png_byte*));

    int i;
    for (i=0; i<(*height); i++)
      row_ptrs[i] = pixels + ((*height) - 1 - i)*rowbytes;

    png_read_image(png_ptr, row_ptrs);

    free(row_ptrs);
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    fclose(png_file);

    return (char *)pixels;
}


Thanks, and I do apologize for the excessively long post.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #2
I added some code tags to make your code easier to read. And since it seems approved posts sometimes slip through the "new posts" listing for new members, I'll bump it so maybe someone else can take a crack at it.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Xcode build issue job1984 4 3,606 Sep 3, 2009 08:18 AM
Last Post: job1984
  My next thing Duane 1 2,074 Aug 8, 2008 07:24 AM
Last Post: Blacktiger
  Internal Error on XCode softengg 1 2,637 Jan 23, 2008 11:40 PM
Last Post: OneSadCookie
  What's the best, erm, window handling thing? ferum 7 3,738 Aug 7, 2006 11:06 AM
Last Post: Duane
  C++ Different forms of the same thing unknown 7 3,260 Jul 5, 2006 11:03 AM
Last Post: Taxxodium