Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. runtime error due to corruption of heap

runtime error due to corruption of heap

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 9350237
    wrote on last edited by
    #1

    Hi Guys!, I'm getting a runtime error, and compiler is pointing at "free.c", shown below:

    void __cdecl _free_base (void * pBlock)
    {

        int retval = 0;
    
    
        if (pBlock == NULL)
            return;
    
        RTCCALLBACK(\_RTC\_Free\_hook, (pBlock, 0));
        retval = HeapFree(\_crtheap, 0, pBlock);
        if (retval == 0)
        {
            errno = \_get\_errno\_from\_oserr(GetLastError());
        }
    

    }

    I couldn't troubleshoot where exactly is the issue.. It would be of great help if you can give some suggestions.. Thanks!

    M D J 3 Replies Last reply
    0
    • M Member 9350237

      Hi Guys!, I'm getting a runtime error, and compiler is pointing at "free.c", shown below:

      void __cdecl _free_base (void * pBlock)
      {

          int retval = 0;
      
      
          if (pBlock == NULL)
              return;
      
          RTCCALLBACK(\_RTC\_Free\_hook, (pBlock, 0));
          retval = HeapFree(\_crtheap, 0, pBlock);
          if (retval == 0)
          {
              errno = \_get\_errno\_from\_oserr(GetLastError());
          }
      

      }

      I couldn't troubleshoot where exactly is the issue.. It would be of great help if you can give some suggestions.. Thanks!

      M Offline
      M Offline
      Member 9350237
      wrote on last edited by
      #2

      This is my main program..I'm sorry to post whole bunch of code here!, but I'm wondering this may helps someone can pointout the issue

      int main(int argc, char* argv[])
      {

      char fname\[30\];
      char fpath\[100\]; // file name for logging
      char fpath1\[100\]; // file name for logging
      char fpath2\[100\];
      
      
      // implimentation for genrating force enable bool array
      memset(boolArray,0,sizeof(bool)\*arraySize);
      
      bool \*pBool =boolArray + 10;
      srand( (unsigned)time(NULL));
      int rIndex2;
      
      for(int i=0;i<10;++i){
      
      	memset(pBool,1,sizeof(bool)\*12);
      	//memset(pBool,1,sizeof(bool));
      	int rIndex = rand()%12;
      
      	pBool\[rIndex\]=0;
      		do{
      			//int rIndex2;
      		
      			rIndex2 = rand()%12;
      			pBool\[rIndex2\]=0;
      	      } while (rIndex2==rIndex);
      	//pBool = rand()%2;
      	pBool +=12;
      }
       
      for (int j=0; jgetNumDevices();
      
      // limit the number of devices to MAX\_DEVICES
      numHapticDevices = cMin(numHapticDevices, MAX\_DEVICES);
      
      
      //-----------------------------------------------------------------------
      // 3D - SCENEGRAPH
      //-----------------------------------------------------------------------
      
      
      //Seperated into two if s
      
      D 1 Reply Last reply
      0
      • M Member 9350237

        Hi Guys!, I'm getting a runtime error, and compiler is pointing at "free.c", shown below:

        void __cdecl _free_base (void * pBlock)
        {

            int retval = 0;
        
        
            if (pBlock == NULL)
                return;
        
            RTCCALLBACK(\_RTC\_Free\_hook, (pBlock, 0));
            retval = HeapFree(\_crtheap, 0, pBlock);
            if (retval == 0)
            {
                errno = \_get\_errno\_from\_oserr(GetLastError());
            }
        

        }

        I couldn't troubleshoot where exactly is the issue.. It would be of great help if you can give some suggestions.. Thanks!

        D Offline
        D Offline
        Daniel Pfeffer
        wrote on last edited by
        #3

        This sort of error is caused by code overwriting the beginning (or end) of a heap block, and corrupting the heap control structures. One way to find such a problem is to wrap each of your blocks in a "signature" (see below), and periodically check that the "signature" is valid. Original structure:

        class foo
        {
        int baz;
        char bar;
        };

        New structure:

        class foo
        {
        long sig1;
        int baz;
        char bar;
        long sig2;
        };

        Note that you can use the constructor to initialize the signature, and the destructor to check whether the signatures have changed. Your methods can check the validity of the signatures before performing any operations. You can also modify the signatures in the destructor, in order to catch accesses to freed memory. A library of this sort is a basic tool for any C++ programmer. If you don't have such a library in your toolbox - write one!

        If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

        1 Reply Last reply
        0
        • M Member 9350237

          This is my main program..I'm sorry to post whole bunch of code here!, but I'm wondering this may helps someone can pointout the issue

          int main(int argc, char* argv[])
          {

          char fname\[30\];
          char fpath\[100\]; // file name for logging
          char fpath1\[100\]; // file name for logging
          char fpath2\[100\];
          
          
          // implimentation for genrating force enable bool array
          memset(boolArray,0,sizeof(bool)\*arraySize);
          
          bool \*pBool =boolArray + 10;
          srand( (unsigned)time(NULL));
          int rIndex2;
          
          for(int i=0;i<10;++i){
          
          	memset(pBool,1,sizeof(bool)\*12);
          	//memset(pBool,1,sizeof(bool));
          	int rIndex = rand()%12;
          
          	pBool\[rIndex\]=0;
          		do{
          			//int rIndex2;
          		
          			rIndex2 = rand()%12;
          			pBool\[rIndex2\]=0;
          	      } while (rIndex2==rIndex);
          	//pBool = rand()%2;
          	pBool +=12;
          }
           
          for (int j=0; jgetNumDevices();
          
          // limit the number of devices to MAX\_DEVICES
          numHapticDevices = cMin(numHapticDevices, MAX\_DEVICES);
          
          
          //-----------------------------------------------------------------------
          // 3D - SCENEGRAPH
          //-----------------------------------------------------------------------
          
          
          //Seperated into two if s
          
          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Why not remove everything that is not related to the problem, as it is just noise. Then we can look at what's left and make suggestions.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          1 Reply Last reply
          0
          • M Member 9350237

            Hi Guys!, I'm getting a runtime error, and compiler is pointing at "free.c", shown below:

            void __cdecl _free_base (void * pBlock)
            {

                int retval = 0;
            
            
                if (pBlock == NULL)
                    return;
            
                RTCCALLBACK(\_RTC\_Free\_hook, (pBlock, 0));
                retval = HeapFree(\_crtheap, 0, pBlock);
                if (retval == 0)
                {
                    errno = \_get\_errno\_from\_oserr(GetLastError());
                }
            

            }

            I couldn't troubleshoot where exactly is the issue.. It would be of great help if you can give some suggestions.. Thanks!

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            This is usually sourced by writing to an array with an out of bound index. So you must check all your allocated arrays (which may include arrays that are allocated by library functions). Starting with your own ones, search for any call to new as array; e.g.:

            type arrayName = new type[size];

            Then for each array check the accesses for invalid indexes (index < 0 or >= size). You can do this manually by reading your source or by guarding all accesses with assertions in debug builds:

            assert(index > 0 && index < arrayNameSize);
            arrayName[index] = someValue;

            Your posted code does not contain the allocation of most. So it can't be checked by us. But there are many candidates: boolArray and all arrays used inside the 'while (i < numHapticDevices)' loop (e.g. hapticDevices).

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups