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. Any alternative for IsBadReadPtr / IsBadWritePtr

Any alternative for IsBadReadPtr / IsBadWritePtr

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
13 Posts 8 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.
  • S Offline
    S Offline
    sw thi
    wrote on last edited by
    #1

    i need to check whether a pointer is valid or not ie., whether its NULL or Bad Ptr. I've searched the net and found these functions IsBadReadPtr or IsBadWritePtr, but they also say that its not good practice to use these functions. Can anyone please tell me are there any alternatives for these function? if there are no alternatives then what are the problems that i might face if i use these function?? Thanks for any help.

    C M D T S 5 Replies Last reply
    0
    • S sw thi

      i need to check whether a pointer is valid or not ie., whether its NULL or Bad Ptr. I've searched the net and found these functions IsBadReadPtr or IsBadWritePtr, but they also say that its not good practice to use these functions. Can anyone please tell me are there any alternatives for these function? if there are no alternatives then what are the problems that i might face if i use these function?? Thanks for any help.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      From where is this pointer coming from ?? In general, when playing with pointers, the thing you have to pay attention to is to initialize them to NULL. Then, you can simply check if the pointer is NULL or not using if (pPointer) I don't see why you need to check for bad pointers...


      Cédric Moonen Software developer
      Charting control [v1.2]

      S 1 Reply Last reply
      0
      • S sw thi

        i need to check whether a pointer is valid or not ie., whether its NULL or Bad Ptr. I've searched the net and found these functions IsBadReadPtr or IsBadWritePtr, but they also say that its not good practice to use these functions. Can anyone please tell me are there any alternatives for these function? if there are no alternatives then what are the problems that i might face if i use these function?? Thanks for any help.

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #3

        IsBadReadPtr / IsBadWritePtr are as good as it gets on Win32 I'm afraid. You shouldn't run into too much trouble as long as your app is doing 'ordinary' things with memory i.e. no mucking around with non-paged pool or using exception filters to allocate virtual memory. I wouldn't try using them against anything you expect to be allocated on the stack either as compiler optimisations might change behaviour between Debug and Release builds. That leaves you with IsBadReadPtr / IsBadWritePtr are fine for Heap memory you allocate yourself i.e. new and delete. I use a special class to check pointer parameters like this when they are passed. The template class has assignment from and cast to operators for the original pointer type. The cast calls the relevant check function whenever it is used. The checking classes are removed in the Release build for performance by the use of a macro. so void SomeFunc( CNullPointerCheck pszData ) which checks pszData for NULL on every call in Debug, becomes void SomeFunc( char* pszData ) with zero overhead in Release builds. It gets written as void SomeFunc( _PC_NP(char*) pszData ) which makes it clear this is a Parameter Check for Null Pointer Other classes check for Readable and Writable memory of a certain size at the target of the pointer using the IsBadReadPtr/IsBadWritePtr functions so void SomeFunc( _PC_WP(char*, 40) pszData ) for example checks for at least 40 characters of writable memory. I'll post the whole lot as an article some day when I'm happy with the error reporting system.

        Nothing is exactly what it seems but everything with seems can be unpicked.

        S S 2 Replies Last reply
        0
        • S sw thi

          i need to check whether a pointer is valid or not ie., whether its NULL or Bad Ptr. I've searched the net and found these functions IsBadReadPtr or IsBadWritePtr, but they also say that its not good practice to use these functions. Can anyone please tell me are there any alternatives for these function? if there are no alternatives then what are the problems that i might face if i use these function?? Thanks for any help.

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

          sw@thi wrote:

          i need to check whether a pointer is valid or not...

          Why?


          "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

          L 1 Reply Last reply
          0
          • S sw thi

            i need to check whether a pointer is valid or not ie., whether its NULL or Bad Ptr. I've searched the net and found these functions IsBadReadPtr or IsBadWritePtr, but they also say that its not good practice to use these functions. Can anyone please tell me are there any alternatives for these function? if there are no alternatives then what are the problems that i might face if i use these function?? Thanks for any help.

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            sw@thi wrote:

            they also say that its not good practice to use these functions.

            any particular reason!

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
            Never mind - my own stupidity is the source of every "problem" - Mixture

            cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

            1 Reply Last reply
            0
            • D David Crow

              sw@thi wrote:

              i need to check whether a pointer is valid or not...

              Why?


              "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

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              New day, same old story ;)

              1 Reply Last reply
              0
              • M Matthew Faithfull

                IsBadReadPtr / IsBadWritePtr are as good as it gets on Win32 I'm afraid. You shouldn't run into too much trouble as long as your app is doing 'ordinary' things with memory i.e. no mucking around with non-paged pool or using exception filters to allocate virtual memory. I wouldn't try using them against anything you expect to be allocated on the stack either as compiler optimisations might change behaviour between Debug and Release builds. That leaves you with IsBadReadPtr / IsBadWritePtr are fine for Heap memory you allocate yourself i.e. new and delete. I use a special class to check pointer parameters like this when they are passed. The template class has assignment from and cast to operators for the original pointer type. The cast calls the relevant check function whenever it is used. The checking classes are removed in the Release build for performance by the use of a macro. so void SomeFunc( CNullPointerCheck pszData ) which checks pszData for NULL on every call in Debug, becomes void SomeFunc( char* pszData ) with zero overhead in Release builds. It gets written as void SomeFunc( _PC_NP(char*) pszData ) which makes it clear this is a Parameter Check for Null Pointer Other classes check for Readable and Writable memory of a certain size at the target of the pointer using the IsBadReadPtr/IsBadWritePtr functions so void SomeFunc( _PC_WP(char*, 40) pszData ) for example checks for at least 40 characters of writable memory. I'll post the whole lot as an article some day when I'm happy with the error reporting system.

                Nothing is exactly what it seems but everything with seems can be unpicked.

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                You should avoid IsBadReadPtr and IsBadWrite pointer like the plague. See here[^] and here[^]. These functions may be "as it gets on Win32", but they fall well short of good enough.

                Steve

                M 1 Reply Last reply
                0
                • S sw thi

                  i need to check whether a pointer is valid or not ie., whether its NULL or Bad Ptr. I've searched the net and found these functions IsBadReadPtr or IsBadWritePtr, but they also say that its not good practice to use these functions. Can anyone please tell me are there any alternatives for these function? if there are no alternatives then what are the problems that i might face if i use these function?? Thanks for any help.

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  You should avoid IsBadReadPtr and IsBadWrite pointer like the plague. See here[^] and here[^]. If you get a bad pointer the best thing that you can do is ***NOT*** attempt to validate it and let the application crash so it can be debugged.

                  Steve

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    From where is this pointer coming from ?? In general, when playing with pointers, the thing you have to pay attention to is to initialize them to NULL. Then, you can simply check if the pointer is NULL or not using if (pPointer) I don't see why you need to check for bad pointers...


                    Cédric Moonen Software developer
                    Charting control [v1.2]

                    S Offline
                    S Offline
                    sw thi
                    wrote on last edited by
                    #9

                    Thanks for your reply. here is why i think i need to check for bad ptr: theres this function in a class that would return a pointer. something like this CMyWnd *GetMyWnd(){ return m_myWnd}; now when this object is destroyed m_myWnd is delete and the pointer is set to NULL, but after the object is destroyed in someother function somewhere this function is again called then GetMyWnd retunrs a CMyWnd* with a junk value. when this pointer is set to NULL in the destructor, it should return NULL but it doesnt, it returns something like 0xcdcdcdcd in the calling function say we do something like this GetMyWnd()->SetSomeFunc(); // the application crashes here, in VS2005 where as in 2003 it works fine. this happens because the pointer that GetMyWnd returns is not a valid pointer and once it tries to acces any member of CMyWnd the application crashes. i've changed the above statement to something like this: CMyWnd *pWnd = GetMyWnd(); // i also tried CMyWnd *pWnd = NULL; pWnd = GetMyWnd(); if(pWnd) { pWnd->SetSomeFunc(); } to see what GetMyWnd returns. It returns a Junk value, and the if condition is evaluated to true and it enters inside and crashes. this is the reason that i thought i should check for NULL as well as Bad Ptr. how do i solve this?? is there anyother way??

                    J 1 Reply Last reply
                    0
                    • M Matthew Faithfull

                      IsBadReadPtr / IsBadWritePtr are as good as it gets on Win32 I'm afraid. You shouldn't run into too much trouble as long as your app is doing 'ordinary' things with memory i.e. no mucking around with non-paged pool or using exception filters to allocate virtual memory. I wouldn't try using them against anything you expect to be allocated on the stack either as compiler optimisations might change behaviour between Debug and Release builds. That leaves you with IsBadReadPtr / IsBadWritePtr are fine for Heap memory you allocate yourself i.e. new and delete. I use a special class to check pointer parameters like this when they are passed. The template class has assignment from and cast to operators for the original pointer type. The cast calls the relevant check function whenever it is used. The checking classes are removed in the Release build for performance by the use of a macro. so void SomeFunc( CNullPointerCheck pszData ) which checks pszData for NULL on every call in Debug, becomes void SomeFunc( char* pszData ) with zero overhead in Release builds. It gets written as void SomeFunc( _PC_NP(char*) pszData ) which makes it clear this is a Parameter Check for Null Pointer Other classes check for Readable and Writable memory of a certain size at the target of the pointer using the IsBadReadPtr/IsBadWritePtr functions so void SomeFunc( _PC_WP(char*, 40) pszData ) for example checks for at least 40 characters of writable memory. I'll post the whole lot as an article some day when I'm happy with the error reporting system.

                      Nothing is exactly what it seems but everything with seems can be unpicked.

                      S Offline
                      S Offline
                      sw thi
                      wrote on last edited by
                      #10

                      I'll look forward to that article. We are not doing anything special with that ptr. just check for that pointer and then if its valid then call one more member from that ptr. i gave a full explanation in reply to cedric moonen's message above yours. Thanks for your reply.

                      1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        You should avoid IsBadReadPtr and IsBadWrite pointer like the plague. See here[^] and here[^]. These functions may be "as it gets on Win32", but they fall well short of good enough.

                        Steve

                        M Offline
                        M Offline
                        Matthew Faithfull
                        wrote on last edited by
                        #11

                        Very interesting. It would seems that you should only use IsBad*Ptr if you can gaurentee 2 things. 1 You're not intentionally passing in exotic memory types like guard page addresses. 2 If anything goes wrong the app will stop because the result of the call will always be acted on. If you're writing the Win32 API you can't make these gaurentees so you shouldn't use the functions. In my case I use them in a framework where I can gaurentee both things so I feel reasonably justified in making this limited use.:)

                        Nothing is exactly what it seems but everything with seems can be unpicked.

                        S 1 Reply Last reply
                        0
                        • S sw thi

                          Thanks for your reply. here is why i think i need to check for bad ptr: theres this function in a class that would return a pointer. something like this CMyWnd *GetMyWnd(){ return m_myWnd}; now when this object is destroyed m_myWnd is delete and the pointer is set to NULL, but after the object is destroyed in someother function somewhere this function is again called then GetMyWnd retunrs a CMyWnd* with a junk value. when this pointer is set to NULL in the destructor, it should return NULL but it doesnt, it returns something like 0xcdcdcdcd in the calling function say we do something like this GetMyWnd()->SetSomeFunc(); // the application crashes here, in VS2005 where as in 2003 it works fine. this happens because the pointer that GetMyWnd returns is not a valid pointer and once it tries to acces any member of CMyWnd the application crashes. i've changed the above statement to something like this: CMyWnd *pWnd = GetMyWnd(); // i also tried CMyWnd *pWnd = NULL; pWnd = GetMyWnd(); if(pWnd) { pWnd->SetSomeFunc(); } to see what GetMyWnd returns. It returns a Junk value, and the if condition is evaluated to true and it enters inside and crashes. this is the reason that i thought i should check for NULL as well as Bad Ptr. how do i solve this?? is there anyother way??

                          J Offline
                          J Offline
                          JudyL_MD
                          wrote on last edited by
                          #12

                          sw@thi wrote:

                          now when this object is destroyed m_myWnd is delete and the pointer is set to NULL, but after the object is destroyed in someother function somewhere this function is again called then GetMyWnd retunrs a CMyWnd* with a junk value. when this pointer is set to NULL in the destructor, it should return NULL but it doesnt, it returns something like 0xcdcdcdcd in the calling function say we do something like this

                          You know what your real problem is - you say so. Fix the real problem instead of trying to use a function to cover up the error. cdcdcdcd is a value that indicates an uninitialized variable. It appears that your GetMyWnd function is not maintaining the value of m_myWnd. Figure out why Judy

                          1 Reply Last reply
                          0
                          • M Matthew Faithfull

                            Very interesting. It would seems that you should only use IsBad*Ptr if you can gaurentee 2 things. 1 You're not intentionally passing in exotic memory types like guard page addresses. 2 If anything goes wrong the app will stop because the result of the call will always be acted on. If you're writing the Win32 API you can't make these gaurentees so you shouldn't use the functions. In my case I use them in a framework where I can gaurentee both things so I feel reasonably justified in making this limited use.:)

                            Nothing is exactly what it seems but everything with seems can be unpicked.

                            S Offline
                            S Offline
                            Stephen Hewitt
                            wrote on last edited by
                            #13

                            I’ve actually fallen victim to a stack guard-page corruption caused by the numerous calls to IsBadReadPtr and IsBadWritePtr in MFC. It wasn’t at all obvious that the memory page in question was on the stack and not the heap (it took ages to even discover that the guard page on the stack was corrupted); after all every local variable is on the stack and many such variables are passed “through” MFC. It took many weeks to debug the issue. Anyway, what’s the point of a function succeeding if it’s called with some random pointer and thus can’t access the data it needs to do its job? Don’t validate memory, crash! Then the source of the problem is staring you in the face instead of some problem far removed from the actual source.

                            Steve

                            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