Possible causes for the 'delete ptr;' to core??

Discussion in 'C++' started by qazmlp1209@rediffmail.com, Jan 11, 2007.

  1. Guest

    My program cores at the 'delete ptr;' statement.
    I could think of the following possibilities for the core/undefined
    behaviour:
    - delete twice
    - new/malloc is done, but subsequently free/delete is used.
    - 'new' is done; Pointer reference is changed; 'delete' is done;

    What else can be the reason?
     
    , Jan 11, 2007
    #1
    1. Advertising

  2. Ivan Novick Guest

    wrote:
    > My program cores at the 'delete ptr;' statement.
    > I could think of the following possibilities for the core/undefined
    > behaviour:
    > - delete twice
    > - new/malloc is done, but subsequently free/delete is used.
    > - 'new' is done; Pointer reference is changed; 'delete' is done;
    >
    > What else can be the reason?

    You can find the reason your self by running your program with a memory
    checker... for example this one http://www.valgrind.org

    ----
    Ivan
    http://www.0x4849.net
     
    Ivan Novick, Jan 11, 2007
    #2
    1. Advertising

  3. wrote:
    > My program cores at the 'delete ptr;' statement.
    > I could think of the following possibilities for the core/undefined
    > behaviour:
    > - delete twice
    > - new/malloc is done, but subsequently free/delete is used.
    > - 'new' is done; Pointer reference is changed; 'delete' is done;
    >
    > What else can be the reason?


    Using delete on a pointer not obtained from new, eg.
    - from malloc,
    - from new[],
    - from somewhere else (that boils down to your case 3).

    Or, if ptr points to an instance of the class, perhaps
    the destructor is to blame.

    As Ivan pointed out, if you are on a x86, you may try
    valgrind to pinpoint the problem easily.

    HTH,
    - J.
     
    Jacek Dziedzic, Jan 11, 2007
    #3
  4. <> wrote in message
    news:...
    > My program cores at the 'delete ptr;' statement.
    > I could think of the following possibilities for the core/undefined
    > behaviour:
    > - delete twice
    > - new/malloc is done, but subsequently free/delete is used.
    > - 'new' is done; Pointer reference is changed; 'delete' is done;
    >
    > What else can be the reason?
    >


    Another reason might be writing before or beyond the memory buffer ptr
    points to (thus overwriting heap management data)

    - Sylvester
     
    Sylvester Hesp, Jan 11, 2007
    #4
  5. Ron Natalie Guest

    wrote:
    > My program cores at the 'delete ptr;' statement.
    > I could think of the following possibilities for the core/undefined
    > behaviour:
    > - delete twice
    > - new/malloc is done, but subsequently free/delete is used.
    > - 'new' is done; Pointer reference is changed; 'delete' is done;
    >
    > What else can be the reason?
    >


    Another popular misbehavior is writing off the end of the
    allocation, which corrupts the memory allocator:
    i.e.,
    char* x = new char[10];
    strcpy(x, "abcdefghijklmnopqrstuvwxyz", 26);
     
    Ron Natalie, Jan 11, 2007
    #5
  6. "Ron Natalie" <> wrote in message
    news:...
    > wrote:
    >> My program cores at the 'delete ptr;' statement.
    >> I could think of the following possibilities for the core/undefined
    >> behaviour:
    >> - delete twice
    >> - new/malloc is done, but subsequently free/delete is used.
    >> - 'new' is done; Pointer reference is changed; 'delete' is done;
    >>
    >> What else can be the reason?
    >>

    >
    > Another popular misbehavior is writing off the end of the
    > allocation, which corrupts the memory allocator:
    > i.e.,
    > char* x = new char[10];
    > strcpy(x, "abcdefghijklmnopqrstuvwxyz", 26);


    Yes, or before the start, like I already said
    In your example: x[-2] = 'a';
    Granted, this happens less often, but is not less important :)

    - Sylvester
     
    Sylvester Hesp, Jan 11, 2007
    #6
  7. Noah Roberts Guest

    Sylvester Hesp wrote:
    > "Ron Natalie" <> wrote in message
    > news:...
    > > wrote:
    > >> My program cores at the 'delete ptr;' statement.
    > >> I could think of the following possibilities for the core/undefined
    > >> behaviour:
    > >> - delete twice
    > >> - new/malloc is done, but subsequently free/delete is used.
    > >> - 'new' is done; Pointer reference is changed; 'delete' is done;
    > >>
    > >> What else can be the reason?
    > >>

    > >
    > > Another popular misbehavior is writing off the end of the
    > > allocation, which corrupts the memory allocator:
    > > i.e.,
    > > char* x = new char[10];
    > > strcpy(x, "abcdefghijklmnopqrstuvwxyz", 26);

    >
    > Yes, or before the start, like I already said
    > In your example: x[-2] = 'a';
    > Granted, this happens less often, but is not less important :)


    It is the more difficult to find for sure and can explode deep inside
    libary objects like vector or string. There is often no relationship
    at all between the object that got overwritten and the one that
    explodes. This type of bug can take hours or days to track down even
    with the right tools. VS has settings you can make that check for
    overruns and there are debugging applications that will do the same.
     
    Noah Roberts, Jan 11, 2007
    #7
    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. Sid
    Replies:
    5
    Views:
    1,084
  2. Heiko Vogel
    Replies:
    3
    Views:
    565
    Method Man
    Sep 14, 2004
  3. franco ziade

    const ptr to const ptr ?

    franco ziade, Feb 17, 2005, in forum: C Programming
    Replies:
    3
    Views:
    402
    franco ziade
    Feb 17, 2005
  4. G Fernandes
    Replies:
    9
    Views:
    602
    DHOLLINGSWORTH2
    Feb 27, 2005
  5. Jason

    difference between *ptr++ and ++*ptr ?

    Jason, May 15, 2005, in forum: C Programming
    Replies:
    19
    Views:
    6,556
    Chris Torek
    May 19, 2005
Loading...

Share This Page