xcode memory error thing issue

Posts: 195
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:

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

    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;

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

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

    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);

    png_infop info_ptr = png_create_info_struct(png_ptr);

    png_infop end_info = png_create_info_struct(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)

    if (bit_depth == 16)
    if(color_type == PNG_COLOR_TYPE_PALETTE)
    else if(color_type == PNG_COLOR_TYPE_GRAY ||
            color_type == PNG_COLOR_TYPE_GRAY_ALPHA)

    if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
        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);

    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);

    return (char *)pixels;

Thanks, and I do apologize for the excessively long post.
Quote this message in a reply
Posts: 3,591
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 4,846 Sep 3, 2009 08:18 AM
Last Post: job1984
  My next thing Duane 1 2,838 Aug 8, 2008 07:24 AM
Last Post: Blacktiger
  Internal Error on XCode softengg 1 3,241 Jan 23, 2008 11:40 PM
Last Post: OneSadCookie
  What's the best, erm, window handling thing? ferum 7 5,099 Aug 7, 2006 11:06 AM
Last Post: Duane
  C++ Different forms of the same thing unknown 7 4,434 Jul 5, 2006 11:03 AM
Last Post: Taxxodium