Strange mmap error..

Discussion in 'Mac Programming' started by gfroyle, Nov 16, 2008.

  1. gfroyle macrumors member

    Feb 8, 2004
    I have a C program that calls "calloc" to get clean memory and then frees it when no longer needed.

    About 95% of the time, the program runs fine and seems to be correct.

    About 5% of the time, it IMMEDIATELY crashes with the following error

    malloc: *** mmap(size=3568361472) failed (error code=12)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug

    I don't call malloc directly (though calloc calls malloc) and I certainly am never asking for 3.5Gb of space, so somehow the whole request seems to have gone awry.

    If I simply run the program again, making no changes to anything, then it usually works the next time, so it appears to be a RANDOM and TRANSIENT error.

    Nevertheless I would prefer to know why it is doing this, and how it can be stopped..

    I am using

    gcc (GCC) 4.2.2
    Copyright (C) 2007 Free Software Foundation, Inc.

    on an Intel Mac running MacOS X 10.5.5


  2. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    What is the code snippit that's producing the error? That looks like a GIGO problem.
  3. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    set a breakpoint in malloc_error_break and check your program state; perhaps you're calling calloc with an uninitialized variable in some edge case?
  4. gfroyle thread starter macrumors member

    Feb 8, 2004

    Yes a GIGO error indeed... I had modified the program so that a variable that was previously read from the command line was instead part of each line of input.

    But I had left in a stray line of code that attempted to USE the variable before any lines of input were processed... so it contained garbage. If it happened by luck to contain 0 then nothing bad happened, but otherwise crash...

    I can't believe I could read my code 50 times and not spot this before making a fool of myself in public :-(

    Oh well..

  5. yeroen macrumors 6502a


    Mar 8, 2007
    Cambridge, MA
    Actually, it's refreshing to see a well-posed question on these boards for a change. Lately it's all been nothing but pleas for homework solutions and fractured-english non-questions like 'i want write operating system please to explain how xcode'
  6. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    Heh, those are always the worst issues. I think I spend a good day or two one a chunk of code with a similar project; had an off-by-one error in a mod op which, in turn, caused my code to occasionally scribble all over one of my interpreter's core data structures. The problem was made even worse because I was running the code an ATMega microcontroller; at least with a standard OS you have some sort of memory protection...

Share This Page