Debug the Heap

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

///EDIT: I did a little more looking and found a function _CrtCheckMemory(), I through it in and found that I was getting a corruption after the first run through of the code listed below. Still not 100% sure why though.

I tracked it down a little further to a for loop that adds information to an array. This sounds like what I've been reading on, however the for statement stays within bounds. And it only has a heap corruption after it exits the loop, I stuck a _CrtCheckMemory() at the end of the inside of the loop and right after it. The only thing that changes after that is the incrementing var increase by one.
Code: [ Select ]
    for (i=0;i<size;i++)            ///> Some crazy heap problem in here
    {                               ///> Some crazy heap problem in here
        cc = *(c+size-i-1);         ///> Some crazy heap problem in here
        k = atoi(&cc);              ///> Some crazy heap problem in here
        this->mStorage[i] = k;      ///> Some crazy heap problem in here
        _CrtCheckMemory();          ///> Some crazy heap problem in here
    }                               ///> Some crazy heap problem in here
    _CrtCheckMemory(); //---> This one notifies me of the heap error.
  1.     for (i=0;i<size;i++)            ///> Some crazy heap problem in here
  2.     {                               ///> Some crazy heap problem in here
  3.         cc = *(c+size-i-1);         ///> Some crazy heap problem in here
  4.         k = atoi(&cc);              ///> Some crazy heap problem in here
  5.         this->mStorage[i] = k;      ///> Some crazy heap problem in here
  6.         _CrtCheckMemory();          ///> Some crazy heap problem in here
  7.     }                               ///> Some crazy heap problem in here
  8.     _CrtCheckMemory(); //---> This one notifies me of the heap error.




Okay, so I'm running into a problem. I have a function that will "resize" an array for my BigInt class. The function works perfectly fine from what I can see. There just seems to be some bug that gets introduced someplace. After I try to delete some memory I get a Heap Corruption error. Now I've done some reading and found that almost %90 of the time the corruption happens before where ever the compile shows the error to be. I've tracked down the error to occur after try to delete a block of memory.

This is the error I get:
Quote:
HEAP CORRUPTION DETECTED: after Normal block (#126) at 0x00B44D40
CRT detected that the application wrote to memory after end of heap buffer.


This is the function:
Code: [ Select ]
void BigInt::resize ( unsigned long int newSize )
{
    unsigned long int i = 0;
    unsigned long int goTo = this->mSize;
 
    short int * oldStorage = this->mStorage;
 
    if ( newSize < this->mSize )
    {
        goTo = newSize;
    }
 
    short int * tmpStorage = new short int [newSize];
 
    for (i=0;i<newSize;i++)
    {
        if( i < this->mSize )
        {
            tmpStorage[i] = this->mStorage[i];
        }
        else
        {
            tmpStorage[i] = 0;
        }
    }
 
    this->mStorage = tmpStorage;
    delete [] oldStorage; // errors after this line executes
 
    this->mSize = newSize;
}
  1. void BigInt::resize ( unsigned long int newSize )
  2. {
  3.     unsigned long int i = 0;
  4.     unsigned long int goTo = this->mSize;
  5.  
  6.     short int * oldStorage = this->mStorage;
  7.  
  8.     if ( newSize < this->mSize )
  9.     {
  10.         goTo = newSize;
  11.     }
  12.  
  13.     short int * tmpStorage = new short int [newSize];
  14.  
  15.     for (i=0;i<newSize;i++)
  16.     {
  17.         if( i < this->mSize )
  18.         {
  19.             tmpStorage[i] = this->mStorage[i];
  20.         }
  21.         else
  22.         {
  23.             tmpStorage[i] = 0;
  24.         }
  25.     }
  26.  
  27.     this->mStorage = tmpStorage;
  28.     delete [] oldStorage; // errors after this line executes
  29.  
  30.     this->mSize = newSize;
  31. }


The error only occurs after I pass through a few different functions, each of which are passing an object onto another. Would this error come up if I were to delete memory that might be being used some place else? But if that is the case wouldn't it only error when I try to access that section of memory again and not when I'm deleting a section?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 81 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.