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. Checking a valid pointer

Checking a valid pointer

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++graphicsdata-structuresperformance
5 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.
  • E Offline
    E Offline
    Electronic75
    wrote on last edited by
    #1

    Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return; this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails! if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return; It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:)

    J D M D 4 Replies Last reply
    0
    • E Electronic75

      Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return; this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails! if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return; It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:)

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

      If the CMyData data object contains pointers itself, it is perfectally reasonable for AfxIsValidAddress(...) to say that a pointer to the object itself is valid, but it cannot evaluate the object's internals to see if its contained pointers are valid.  For example, the object itself may have stepped on its runtime-type information.    Peace!

      -=- James
      Please rate this message - let me know if I helped or not! * * * 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!
      See DeleteFXPFiles

      1 Reply Last reply
      0
      • E Electronic75

        Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return; this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails! if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return; It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:)

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Electronic75 wrote:

        if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData)))

        Set a breakpoint on this statement. Is m_pData a CMyData object? Does it point to the same address as it did when first initialized?


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        1 Reply Last reply
        0
        • E Electronic75

          Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return; this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails! if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return; It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:)

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          AfxIsValidAddress in VS6 calls IsBadReadPtr or IsBadWritePtr depending on whether bReadWrite is FALSE or TRUE respectively. However, it's been discovered in the last few years that these APIs are seriously flawed. See IsBadXxxPtr should really be called CrashProgramRandomly[^] for details. Visual Studio .NET 2003 still calls IsBadXxxPtr in Debug builds, but in Release builds only checks for NULL. In Visual Studio 2005, the IsBadXxxPtr checks are completely removed, the function only checks for NULL. It does look like you've called through a bad pointer - this could well mean that the virtual function pointer inside CMyData is NULL, which it will be if you used memset, for example.

          Stability. What an interesting concept. -- Chris Maunder

          1 Reply Last reply
          0
          • E Electronic75

            Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return; this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails! if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return; It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:)

            D Offline
            D Offline
            DevMentor org
            wrote on last edited by
            #5

            There is no way to check if an actual non-null pointer is valid! You might want to look at MSDN and read the remake section or learn the limitation of AfxIsValidAddress

            Yours Truly, The One and Only!

            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