malloc error

Discussion in 'C++' started by afarah, Oct 13, 2004.

  1. afarah

    afarah Guest

    Under what condition will we see a segmentation fault in the malloc
    library routine? Specifically, a failure in call to "chunk_alloc()"
    from within malloc.

    I have a multi-threaded c++ program that occasionally core-dumps in
    "malloc" when running on Linux 2.4 (it always works fine on Solaris
    5). The stack trace shows the final call to "chunk_alloc".

    I would like to re-create this bug within a debugger. However, the
    segmentation fault happens so infrequently that I think I need to have
    a better idea of how a problem like this can arise before I can
    re-create it.

    Many thanks in advance for any ideas.
    afarah, Oct 13, 2004
    #1
    1. Advertising

  2. afarah wrote:
    > Under what condition will we see a segmentation fault in the malloc
    > library routine? Specifically, a failure in call to "chunk_alloc()"
    > from within malloc.
    >
    > I have a multi-threaded c++ program that occasionally core-dumps in
    > "malloc" when running on Linux 2.4 (it always works fine on Solaris
    > 5). The stack trace shows the final call to "chunk_alloc".
    >
    > I would like to re-create this bug within a debugger. However, the
    > segmentation fault happens so infrequently that I think I need to have
    > a better idea of how a problem like this can arise before I can
    > re-create it.
    >
    > Many thanks in advance for any ideas.


    Multi-threading is OT here simply because C++ program model is a single
    process on a single CPU with all parts of the program executing in
    a sequence. Intrinsics of C library are too, because they are very
    compiler-specific. Differences between OSes are also beyond the scope
    of this newsgroup. Perhaps you will find better help in a forum for
    your compiler or OS. Try comp.os.linux.development.apps or gnu.g++.help.

    In general a library routine fails when it gets invalid input, and there
    is no invalid input for malloc, IIRC. Any number you pass in is going to
    be interpreted as an unsigned integral value, and the system will either
    allocate the requested amount, in which case a pointer to it is returned,
    or fail to do so, in which case it will return a null pointer.

    In most cases unexpected segmentation faults are due to memory corruption
    that happened some time before the code that breaks is executed. You may
    need some memory checking tools (like Purify) to see what's going wrong
    with the program's memory. Of course, program's being multithreaded
    doesn't really help...

    Good luck!

    Victor
    Victor Bazarov, Oct 13, 2004
    #2
    1. Advertising

  3. afarah wrote:
    > Under what condition will we see a segmentation fault in the malloc
    > library routine? Specifically, a failure in call to "chunk_alloc()"
    > from within malloc.

    If there is a bug in the malloc library.

    More common though, you have a nasty bug which does something very
    bad, probably corrupting the heap,stack or otherwise invoke undefined
    behavior.

    > I have a multi-threaded c++ program that occasionally core-dumps in
    > "malloc" when running on Linux 2.4 (it always works fine on Solaris
    > 5). The stack trace shows the final call to "chunk_alloc".
    >
    > I would like to re-create this bug within a debugger. However, the
    > segmentation fault happens so infrequently that I think I need to have
    > a better idea of how a problem like this can arise before I can
    > re-create it.

    Get a hold of valgrind, and run your program in its memory checker tool.
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Oct 13, 2004
    #3
  4. afarah

    Ron Natalie Guest

    afarah wrote:
    > Under what condition will we see a segmentation fault in the malloc
    > library routine? Specifically, a failure in call to "chunk_alloc()"
    > from within malloc.
    >

    To amplify on what Victor said. The most common problem with malloc()
    and free() exploding is that on a previous allocation you did something
    bad.

    The most common ones are:

    1. Freeing something not malloced, or freeing the same value twice.
    2. (I vote for this one) Writing out of the bounds of a previous malloc.
    Common malloc implementations put control information for the space
    management intermixed with the allocations. Write a few bytes outside
    of a malloc'd region is almost sure to corrupt malloc's internal data.
    Ron Natalie, Oct 13, 2004
    #4
  5. afarah wrote:
    > Under what condition will we see a segmentation fault in the malloc
    > library routine? Specifically, a failure in call to "chunk_alloc()"
    > from within malloc.
    >
    > I have a multi-threaded c++ program that occasionally core-dumps in
    > "malloc" when running on Linux 2.4 (it always works fine on Solaris
    > 5). The stack trace shows the final call to "chunk_alloc".
    >
    > I would like to re-create this bug within a debugger. However, the
    > segmentation fault happens so infrequently that I think I need to have
    > a better idea of how a problem like this can arise before I can
    > re-create it.
    >
    > Many thanks in advance for any ideas.


    It's an app bug.

    Try running the app under valgrind with and set the environment variable
    GLIBCXX_FORCE_NEW (or GLIBCPP_FORCE_NEW depending on version of GCC).

    Or try runnung under efence.
    Gianni Mariani, Oct 14, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    681
  2. ravi
    Replies:
    0
    Views:
    435
  3. Peter
    Replies:
    34
    Views:
    1,893
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    458
    Walter Roberson
    Feb 19, 2005
  5. JPerelli

    malloc rare error (malloc bug??)

    JPerelli, Oct 2, 2009, in forum: C Programming
    Replies:
    18
    Views:
    555
    JPerelli
    Oct 3, 2009
Loading...

Share This Page