free causes core-dump

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • loudking

    free causes core-dump

    Hi, all

    Here is part of my code.

    =============== =============== ==========
    void *record;

    /* treat record */

    if (record) {
    fprintf(stderr, "free record start\n");
    fprintf(stderr, "char= %s\n", (char*)record);
    fprintf(stderr, "hex= %x\n", (u_int)record);
    free(record);
    fprintf(stderr, "free record end\n");
    }
    =============== =============== ==========

    The output is

    =============== =============== ==========
    free record start
    char= @Ýa X¨-
    hex= 92d73e8
    *** glibc detected *** double free or corruption (!prev): 0x092d73e8
    ***
    zsh: 18957 abort (core dumped) ./zizzy system/manager
    =============== =============== ============

    Could anybody tell me what is the reason for core-dump?

    I already checked if record is empty ....
  • Joachim Schmitz

    #2
    Re: free causes core-dump

    loudking wrote:
    Hi, all
    >
    Here is part of my code.
    >
    =============== =============== ==========
    void *record;
    >
    /* treat record */
    >
    if (record) {
    fprintf(stderr, "free record start\n");
    fprintf(stderr, "char= %s\n", (char*)record);
    fprintf(stderr, "hex= %x\n", (u_int)record);
    free(record);
    record=NULL;
    fprintf(stderr, "free record end\n");
    }
    =============== =============== ==========
    >
    The output is
    >
    =============== =============== ==========
    free record start
    char= @Ýa X¨-
    hex= 92d73e8
    *** glibc detected *** double free or corruption (!prev): 0x092d73e8
    ***
    zsh: 18957 abort (core dumped) ./zizzy system/manager
    =============== =============== ============
    >
    Could anybody tell me what is the reason for core-dump?
    >
    I already checked if record is empty ....
    But you didn't set it to NULL after the first free, did you?

    Bye, Jojo


    Comment

    • James Kuyper

      #3
      Re: free causes core-dump

      loudking wrote:
      Hi, all
      >
      Here is part of my code.
      >
      =============== =============== ==========
      void *record;
      >
      /* treat record */
      >
      if (record) {
      fprintf(stderr, "free record start\n");
      fprintf(stderr, "char= %s\n", (char*)record);
      fprintf(stderr, "hex= %x\n", (u_int)record);
      free(record);
      fprintf(stderr, "free record end\n");
      }
      =============== =============== ==========
      >
      The output is
      >
      =============== =============== ==========
      free record start
      char= @Ýa X¨-
      hex= 92d73e8
      *** glibc detected *** double free or corruption (!prev): 0x092d73e8
      ***
      zsh: 18957 abort (core dumped) ./zizzy system/manager
      =============== =============== ============
      >
      Could anybody tell me what is the reason for core-dump?
      As usual, when you post only the part of your program that you think
      contains the error, you're generally wrong.
      You shouldn't call free() unless you've first called malloc() or
      calloc() or realloc(), but I don't see that call in your code. The error
      message implies that there was a previous call free() for the same
      pointer value, but I don't see that previous call to free(). We need at
      least a larger code sample to diagnose this problem. A complete program
      of minimum size that reliably demonstrates the problem would be even better.

      From past experience, I know that when you have problems like this with
      dynamically allocated memory, those problems are often caused by a part
      of the code that has nothing to do with the part of the code where the
      symptoms first become fatal. Look for buffer overruns, attempts to write
      to or read from memory that has already been deallocated, and other
      similar possibilities.

      Comment

      • Keith Thompson

        #4
        Re: free causes core-dump

        loudking <loudking@gmail .comwrites:
        Here is part of my code.
        >
        =============== =============== ==========
        void *record;
        >
        /* treat record */
        >
        if (record) {
        fprintf(stderr, "free record start\n");
        fprintf(stderr, "char= %s\n", (char*)record);
        fprintf(stderr, "hex= %x\n", (u_int)record);
        free(record);
        fprintf(stderr, "free record end\n");
        }
        =============== =============== ==========
        [...]

        See question 7.21 in the comp.lang.c FAQ, <http://c-faq.com/>.

        Also, your method of displaying the address of your pointer is
        incorrect. The "char=" line converts the pointer to char* and tells
        fprintf() to assume that the resulting pointer points to a string, and
        to display the value of that string (this doesn't display the
        address). Judging by the output you got, record probably points to
        some kind of binary data, not a string.

        The hex version converts the void* value to u_int and prints it using
        "%x". (Presumably u_int is unsigned int; why not just use "unsigned
        int" so your readers don't have to guess?) But conversion of a
        pointer to an integer type, though it's allowed doesn't necessarily
        give you a meaningful result.

        Fortunately, fprintf() provides a format for just this purpose: "%p".

        To print a pointer value (other than a function pointer):

        fprintf(stderr, "record=%p\ n", record);

        "%p" requires a void* pointer value. In this case, record is already
        of that type. If it weren't, you'd need to cast it:

        int x;
        fprintf(stderr, "&x = %p\n", (void*)&x);

        --
        Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Keith Thompson

          #5
          Re: free causes core-dump

          Keith Thompson <kst-u@mib.orgwrites :
          loudking <loudking@gmail .comwrites:
          >Here is part of my code.
          >>
          >============== =============== ===========
          > void *record;
          >>
          > /* treat record */
          >>
          > if (record) {
          > fprintf(stderr, "free record start\n");
          > fprintf(stderr, "char= %s\n", (char*)record);
          > fprintf(stderr, "hex= %x\n", (u_int)record);
          > free(record);
          > fprintf(stderr, "free record end\n");
          > }
          >============== =============== ===========
          [...]
          >
          See question 7.21 in the comp.lang.c FAQ, <http://c-faq.com/>.
          >
          Also, your method of displaying the address of your pointer is
          incorrect.
          [...]

          Correction: I should have written "the *value* of your pointer", not
          its address.

          --
          Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          Working...