Calling delete Crashes my Program

This is a discussion on Calling delete Crashes my Program within the C++ Programming forums, part of the General Programming Boards category; I'm am trying to write a little program/algorithm in C++. I have defined my own data type and created an ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    197

    Calling delete Crashes my Program

    I'm am trying to write a little program/algorithm in C++. I have defined my own data type and created an array of this data type using the new operator. I try to return this memory to the system by using the delete operator on my array, but it seems to crash my program. The program compiles okay, but when I run it, I get the message

    2 [sig] a 2216 open_stackdumpfile: Dumping stack trace to a.exe.stackdump
    14277 [sig] a 2216 e:\working\a.exe: *** fatal error - called with threadlist_ix -1
    If I don't use the delete operator then the program runs without crashing. The problem with this, is that I'm under the impression that every new should be matched with a delete. Does anyone know why my program is crashing?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    If you use new, use delete.
    If you use new[], use delete[].

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Without seeing the code, it's almost impossible to say what is going wrong, but "it ain't right" when delete causes your app to crash - and it wouldn't be delete's fault.

    Two things come to mind:
    1. You are overflowing some allocated memory and overwriting some data kept after the memory you allocated.
    2. (Less likely, but possible) you are using new something[...] and not using delete [] ... - if you have square brackets when allocating, you need square brackets on deleting - and the same applies if you DON'T have square brackets in one place, you shouldn't have them in the other.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    u must be pointing to any non allocated memory...using signal(), that will help...

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,524
    Another common cause of a crash on delete is, unfortunately, a pointer molestation somewhere before deleting. I say "unfortunately" because the problem could be ANY of your code that was executed before you attempt to delete your array.

    On of the rules of debugging is that the cause of a crash (i.e. abnormal program termination) is either at the crash site, or it is any code executed BEFORE the crash occurs. Ironically, one of the common mistakes of newbies is to try and find the cause of a crash by stepping FORWARD, in a debugger, from the crash.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    197
    The data type has a destructor that calls delete[]. When I remove this, then the program runs. I don't know if this is useful information or not.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, assuming that you are also using new ... [] then that's the right version of delete.

    The most likely scenario is that you have written past the end of your allocated memory, e.g. :
    Code:
       int *p = new int[10];
       ...
       p[11] = 7;
    This will cause undefned behaviour, which can be "anything", but the most likely scenario is a "crash" of some sort.

    Again, without seeing the code, it's almost impossible to even guess what the actual problem is.

    Try checking all your limits where you are writing to arrays.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by thetinman View Post
    The data type has a destructor that calls delete[]. When I remove this, then the program runs. I don't know if this is useful information or not.
    So assuming you are using new[] and delete[] together, the crash is caused by corruption of the allocator's free list, which means you overflowed a dynamic array somewhere. The problem is not necessarily with the array you are trying to delete[], but you can at least start there.

    Examine all parts of your code where you index arrays, particularly code which writes into them. Look for bounds violations.

    Or, compile your program on Linux and run it under Valgrind, which will immediately pinpoint the exact bug.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> The data type has a destructor that calls delete[].
    Does this class have a copy constructor and copy assignment operator. If you use delete[] in your destructor you will need to either disable copying or implement those two functions. If you don't, your program will crash when you call delete[] in some cases.

    Why are you using new[] and delete[] anyway? You should probably be using vector. If you did, you wouldn't need the code in your destructor, and that wouldn't force you to write the copy functions either.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by Daved View Post
    Why are you using new[] and delete[] anyway? You should probably be using vector. If you did, you wouldn't need the code in your destructor, and that wouldn't force you to write the copy functions either.
    Or at least use boost::scoped_array<>, which auto-deletes the array and is also non-copyable, which prevents you from accidentally writing a class which aliases the array.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Daved View Post
    >> The data type has a destructor that calls delete[].
    Does this class have a copy constructor and copy assignment operator. If you use delete[] in your destructor you will need to either disable copying or implement those two functions. If you don't, your program will crash when you call delete[] in some cases.

    Why are you using new[] and delete[] anyway? You should probably be using vector. If you did, you wouldn't need the code in your destructor, and that wouldn't force you to write the copy functions either.
    The force is strong in this one.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    197
    I am always using new[] and delete[] together. As far as I know, everything is in bounds.

    None of my classes have copy constructors or copy assignment operators. It isn't clear to me that I need these. Maybe I do, since one data type is copied into a member variable (that is the same type as the input) of a different data type.

    I am using new[] and delete[] because vector does not seem to allow me to access elements of arrays arbitrarily. I consider this to be a big drawback.
    Last edited by thetinman; 10-12-2007 at 12:29 PM. Reason: added []

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by thetinman View Post
    None of my classes have copy constructors or copy assignment operators.
    More likely, ALL your classes have copy constructors and assignment operators. If you don't write them, the compiler synthesizes them. If you want to prevent copy construction or assignment, you have to do it explicitly.

    I am using new[] and delete[] because vector does not seem to allow me to access elements of arrays arbitrarily. I consider this to be a big drawback.
    Huh? That's the whole point of vector. Just use [].

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> It isn't clear to me that I need these.
    If you have delete[] in your destructor, then you absolutely need those. That is almost certainly why your program crashes.

    >> I am using new[] and delete[] because vector does not seem to allow me to access elements of arrays arbitrarily.
    I can't think of anything that a dynamic array with new[]/delete[] can do that vector cannot do, and there are a pile of things that vector can do better.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by thetinman View Post
    I am always using new[] and delete[] together. As far as I know, everything is in bounds.

    None of my classes have copy constructors or copy assignment operators. It isn't clear to me that I need these. Maybe I do, since one data type is copied into a member variable (that is the same type as the input) of a different data type.

    I am using new[] and delete[] because vector does not seem to allow me to access elements of arrays arbitrarily. I consider this to be a big drawback.
    Sure you can:
    vector::operator[]

    --
    Mats
    Last edited by matsp; 10-12-2007 at 01:25 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. My I/O Program crashes, I need some help
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 02:16 AM
  4. program crashes
    By Strut in forum Linux Programming
    Replies: 3
    Last Post: 02-10-2002, 10:49 AM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21