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. Strange problem with _CrtIsValidHeapPointer

Strange problem with _CrtIsValidHeapPointer

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresdebugginghelptutorialquestion
9 Posts 5 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
    Mathias S
    wrote on last edited by
    #1

    When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.

    class CData
    {
    public:
    ~CData() { }
    DWORD nValue;
    };

    This works of cause.

    CData* pData = NULL;
    pData = new CData;
    _CrtIsValidHeapPointer( pData );
    delete pData;

    This Does NOT Work

    pData = new CData;
    pData = new CLineInfo[ 2 ];
    _CrtIsValidHeapPointer( pData ); // <<-- fails
    delete[] pData;

    BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.

    V N L J 4 Replies Last reply
    0
    • M Mathias S

      When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.

      class CData
      {
      public:
      ~CData() { }
      DWORD nValue;
      };

      This works of cause.

      CData* pData = NULL;
      pData = new CData;
      _CrtIsValidHeapPointer( pData );
      delete pData;

      This Does NOT Work

      pData = new CData;
      pData = new CLineInfo[ 2 ];
      _CrtIsValidHeapPointer( pData ); // <<-- fails
      delete[] pData;

      BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      How does it work if instead of

      pData = new CData;
      pData = new CLineInfo[ 2 ];
      

      you have

      pData = new CData[ 2 ];?
      
      M 1 Reply Last reply
      0
      • M Mathias S

        When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.

        class CData
        {
        public:
        ~CData() { }
        DWORD nValue;
        };

        This works of cause.

        CData* pData = NULL;
        pData = new CData;
        _CrtIsValidHeapPointer( pData );
        delete pData;

        This Does NOT Work

        pData = new CData;
        pData = new CLineInfo[ 2 ];
        _CrtIsValidHeapPointer( pData ); // <<-- fails
        delete[] pData;

        BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.

        N Offline
        N Offline
        NiceNaidu fo
        wrote on last edited by
        #3

        This error occurs when you alllocate memory in one heap and delete it in another heap. Check whether both Debug and Release mode use run time library settings are the same or not . Appu.. "If you judge people, you have no time to love them."

        M 1 Reply Last reply
        0
        • V Viorel

          How does it work if instead of

          pData = new CData;
          pData = new CLineInfo[ 2 ];
          

          you have

          pData = new CData[ 2 ];?
          
          M Offline
          M Offline
          Mathias S
          wrote on last edited by
          #4

          heh That was a typo when writing the message. it should of cause be pData = new CData[ 2 ];

          1 Reply Last reply
          0
          • M Mathias S

            When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.

            class CData
            {
            public:
            ~CData() { }
            DWORD nValue;
            };

            This works of cause.

            CData* pData = NULL;
            pData = new CData;
            _CrtIsValidHeapPointer( pData );
            delete pData;

            This Does NOT Work

            pData = new CData;
            pData = new CLineInfo[ 2 ];
            _CrtIsValidHeapPointer( pData ); // <<-- fails
            delete[] pData;

            BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.

            L Offline
            L Offline
            Laxman Auti
            wrote on last edited by
            #5

            Mathias S. wrote:

            BUT if I remove the Destructor from the CData class. It WORKS ! Strange...

            Try adding Constructer of CData class.. what is CLineInfo?? Is it derived from CData class. Knock out 't' from can't, You can if you think you can :cool:

            M 1 Reply Last reply
            0
            • N NiceNaidu fo

              This error occurs when you alllocate memory in one heap and delete it in another heap. Check whether both Debug and Release mode use run time library settings are the same or not . Appu.. "If you judge people, you have no time to love them."

              M Offline
              M Offline
              Mathias S
              wrote on last edited by
              #6

              I suspected that too. But if I do new and delete right after each other as in the example. they are run from the same place. I tryed changing runtime library too. I Have tested with both /MTd and /MDd but with no success. and If that was the error. Why does it works if I remove the destructor. It is that part that is messing with my head.. -Mathias S.

              1 Reply Last reply
              0
              • L Laxman Auti

                Mathias S. wrote:

                BUT if I remove the Destructor from the CData class. It WORKS ! Strange...

                Try adding Constructer of CData class.. what is CLineInfo?? Is it derived from CData class. Knock out 't' from can't, You can if you think you can :cool:

                M Offline
                M Offline
                Mathias S
                wrote on last edited by
                #7

                CLineInfo is what the class is called in my real code. That was a cut'n'paste error :) And it have constructor and a lot of variables im my real code, and I created this example and striped it down until _CrtIsValidHeapPointer did not complain anymore. and that happend when I removed the destuctor. I can have a Constructer there without any problems, as long as there is no desructor. -Mathias S.

                1 Reply Last reply
                0
                • M Mathias S

                  When I delete an array of objects I got 'Invalid Address specified to RtlValidateHeap' when compiling with debug info. When delete[] is run in debug mode it calls _CrtIsValidHeapPointer(...) and it fails. So I dug into my code and after a while I found that if I delete an array of objects it fails ONLY if the objects have a destructor ? Why don't it work? Im doing something wrong ? Example.

                  class CData
                  {
                  public:
                  ~CData() { }
                  DWORD nValue;
                  };

                  This works of cause.

                  CData* pData = NULL;
                  pData = new CData;
                  _CrtIsValidHeapPointer( pData );
                  delete pData;

                  This Does NOT Work

                  pData = new CData;
                  pData = new CLineInfo[ 2 ];
                  _CrtIsValidHeapPointer( pData ); // <<-- fails
                  delete[] pData;

                  BUT if I remove the Destructor from the CData class. It WORKS ! Strange... - Mathias S.

                  J Offline
                  J Offline
                  James R Twine
                  wrote on last edited by
                  #8

                  Please correct your post to fix the typos you mention in later posts.    Does the CLineInfo overload its operator new for custom heap/caching/allocation purposes?  If so, that will likely raise the error because the heap management will be different.    If you are allocating in one file and deleting in another, make sure that all files involved have the #define new DEBUG_NEW (VC++ 6.0) at the top of the file, or you could end up using different heap routines.    Peace! -=- James


                  If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                  Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                  DeleteFXPFiles & CheckFavorites (Please rate this post!)

                  M 1 Reply Last reply
                  0
                  • J James R Twine

                    Please correct your post to fix the typos you mention in later posts.    Does the CLineInfo overload its operator new for custom heap/caching/allocation purposes?  If so, that will likely raise the error because the heap management will be different.    If you are allocating in one file and deleting in another, make sure that all files involved have the #define new DEBUG_NEW (VC++ 6.0) at the top of the file, or you could end up using different heap routines.    Peace! -=- James


                    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                    DeleteFXPFiles & CheckFavorites (Please rate this post!)

                    M Offline
                    M Offline
                    Mathias S
                    wrote on last edited by
                    #9

                    No! no overloading of new/delete. But I must have had som other error that is fixed now, that overwrote the memory or something. because delete[] work now. But _CrtIsValidPointer still does not work when checking a pointer of an array of objects with a destructors. Because of a bug in CrtIsValidHeapPointer. but as a workaround I can use _CrtIsValidHeapPointer( ((LPCBYTE)p)-4 ); - Mathias

                    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