Thread: Trying to Free Object On Heap As Array Throwing Debug Asserts

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    41

    Trying to Free Object On Heap As Array Throwing Debug Asserts

    If some code throws a _IsCrtValidPointer or some error along those lines using the debug versions of Free() will that be a problem in the Release versions? Memory Corruption/Leak?

    Ex:

    Code:
    Class Foo
    {
    }
    
    Foo *a[2];
    
    a[1] = new Foo;
    
    delete [] a[1];  <-- Throws the Assert mentioned above 
    // Code above should be delete a[1];
    In debug this throws an assertion, but under Release it runs fine.. what I'm wondering is does the Release version leak or corrupt memory in anyway?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is undefined behavior. It could cause a crash (even if it works this one time). It could corrupt memory and the crash or incorrect output won't occur until later in the program.

    There's no reason to use delete [] there instead of delete.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    There's no reason to use delete [] there instead of delete.
    Yep, I have changed it, but it was something that at the time was a typo/mis-overlooked piece of code and lasted for a long time... Just wondered if it was possibly corrupting the heap the whole time it was like that or if the debug versions of the heap were over-causius (crap my spelling in the toilet tonight) throwing an ASSERT... your answer quenched my thirst for knowledge:

    It is undefined behavior. It could cause a crash (even if it works this one time). It could corrupt memory and the crash or incorrect output won't occur until later in the program.
    Thanks for the fast reply!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. DLL & free() Heap Access Violation
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 08-13-2002, 10:45 PM