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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. validating handles and pointers

validating handles and pointers

Scheduled Pinned Locked Moved C / C++ / MFC
c++debuggingregexperformancehelp
24 Posts 8 Posters 1 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 Electronic75

    The problem is this handle has to be accessed from different threads. I checked its value before executing CloseHandle() it has some value which is not what I assigned at the beginning and also it is not INVALID_HANDLE_VALUE but still after execution of CloseHandle an exception occurs.

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #6

    Did you set up some synchronization mechanism to assure that

    CloseHandle(handle);
    handle = INVALID_HANDLE_VALUE;

    is atomic? :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    [my articles]

    In testa che avete, signor di Ceprano?

    E 1 Reply Last reply
    0
    • E Electronic75

      Hello board, There is a function that checks if a pointer is in valid memory space in MFC, I have used that before but now I forgot that and MSDN help is so crap and stupid that despite that I've tried many keywords it returns completely unrelated subject. No wonder MSN never can match google. Can anybody give me the name of that function, please to check validity of a pointer. Also is there any function that checks validity of a handle. When I use CloseHandle, in debug mode sometimes an exception occurs that says handle value is invalid. while I usually define handle value as INVALID_HANDLE_VALUE in the beginning and my expression is if(m_hHandle != INVALID_HANDLE_VALUE) CloseHandle(m_hHandle); but exception occurs and the value of handle is other than INVALID_HANDLE_VALUE Thanks and have a nice day.

      R Offline
      R Offline
      Rajkumar R
      wrote on last edited by
      #7

      You need to refer the API documentation, The API that returns HANDLE to system resource has different behaviour on return values, for example CreateFile returns INVALID_HANDLE_VALUE while CreateEvent returns NULL on failure, so checking the handle on CloseHandle depends on the API that created the Handle. Are u looking for _CrtIsValidPointer, _CrtIsValidHeapPointer for pointer validation. AfxIsValidAddress (MFC)

      modified on Monday, February 04, 2008 5:52:35 AM

      E 1 Reply Last reply
      0
      • E Electronic75

        To free memory...

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

        And what's the difference with what I suggested (meaning setting your pointer to NULL when you deleted it) ?

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

        E 1 Reply Last reply
        0
        • CPalliniC CPallini

          Did you set up some synchronization mechanism to assure that

          CloseHandle(handle);
          handle = INVALID_HANDLE_VALUE;

          is atomic? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          [my articles]

          E Offline
          E Offline
          Electronic75
          wrote on last edited by
          #9

          Before running CloseHandle() I terminate those threads. Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space. I've used it so many times and I now I just forgot that and I can't find it in MSDN crap.

          C CPalliniC D 3 Replies Last reply
          0
          • E Electronic75

            Before running CloseHandle() I terminate those threads. Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space. I've used it so many times and I now I just forgot that and I can't find it in MSDN crap.

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

            Electronic75 wrote:

            Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space.

            Never used it myself and I don't think I'll ever need it. If you follow the simple rule I gave you, it's totally unnecessary to use it. I would even say it is bad practice because you don't have a "clean" code.

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

            1 Reply Last reply
            0
            • C Cedric Moonen

              And what's the difference with what I suggested (meaning setting your pointer to NULL when you deleted it) ?

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

              E Offline
              E Offline
              Electronic75
              wrote on last edited by
              #11

              do you mean if I simply set pointer to null it frees memory no memory leak no resource leak ...??? so the what's the need for delete we can simply assign null to a pointer. can you refer me to a document, please?

              C 1 Reply Last reply
              0
              • E Electronic75

                do you mean if I simply set pointer to null it frees memory no memory leak no resource leak ...??? so the what's the need for delete we can simply assign null to a pointer. can you refer me to a document, please?

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

                No, you need to delete it and then you set it to NULL. What I meant is, once you set it to NULL, there's no need anymore to check if the pointer is valid: simply check it if it is NULL.

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

                E 1 Reply Last reply
                0
                • R Rajkumar R

                  You need to refer the API documentation, The API that returns HANDLE to system resource has different behaviour on return values, for example CreateFile returns INVALID_HANDLE_VALUE while CreateEvent returns NULL on failure, so checking the handle on CloseHandle depends on the API that created the Handle. Are u looking for _CrtIsValidPointer, _CrtIsValidHeapPointer for pointer validation. AfxIsValidAddress (MFC)

                  modified on Monday, February 04, 2008 5:52:35 AM

                  E Offline
                  E Offline
                  Electronic75
                  wrote on last edited by
                  #13

                  bingo! AfxIsValidAddress() thanks alot RR :) :) :) :) :) :) :) Oh Thanks for the point about handles I have to be more careful,

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    No, you need to delete it and then you set it to NULL. What I meant is, once you set it to NULL, there's no need anymore to check if the pointer is valid: simply check it if it is NULL.

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

                    E Offline
                    E Offline
                    Electronic75
                    wrote on last edited by
                    #14

                    Good point but still before using it, it maybe necessary to check its validity. thanks

                    C D 2 Replies Last reply
                    0
                    • E Electronic75

                      Before running CloseHandle() I terminate those threads. Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space. I've used it so many times and I now I just forgot that and I can't find it in MSDN crap.

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #15

                      AfxIsValidAddress [ ^]?

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      [my articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      • E Electronic75

                        Good point but still before using it, it maybe necessary to check its validity. thanks

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

                        Absolutely not. Why ? If it is NULL, it is NULL and thus invalid. Of course, you have to initialize it to NULL in your constructor too.

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

                        E 1 Reply Last reply
                        0
                        • E Electronic75

                          The problem is this handle has to be accessed from different threads. I checked its value before executing CloseHandle() it has some value which is not what I assigned at the beginning and also it is not INVALID_HANDLE_VALUE but still after execution of CloseHandle an exception occurs.

                          I Offline
                          I Offline
                          Iain Clarke Warrior Programmer
                          wrote on last edited by
                          #17

                          If you're worrying about having an invalid handle between checking for INVALID_HANDL_VALUE and closing it, then why aren't you worrying about the IsValidHandle routine returning TRUE, then the handle becoming invalid? As Cedric has been trying to tell you until he's blue in the face, you NEED some sort of synchronisation (eg Critical Section) if you can open close these from different threads. Also, if you have multiple threads all able to close a handle, then your program is badly tangled already... Good luck, Iain.

                          Iain Clarke appearing by Special Request of CPallini.

                          E 1 Reply Last reply
                          0
                          • C Cedric Moonen

                            Absolutely not. Why ? If it is NULL, it is NULL and thus invalid. Of course, you have to initialize it to NULL in your constructor too.

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

                            E Offline
                            E Offline
                            Electronic75
                            wrote on last edited by
                            #18

                            I think I did not made my point clear, I said before using it. When one defines a pointer surely he wants to use it somewhere it is not null then and it should be checked especially if some naughty functions have played with it before. When one wanted to destroy the pointer then he can assign null to it as you said.

                            J 1 Reply Last reply
                            0
                            • I Iain Clarke Warrior Programmer

                              If you're worrying about having an invalid handle between checking for INVALID_HANDL_VALUE and closing it, then why aren't you worrying about the IsValidHandle routine returning TRUE, then the handle becoming invalid? As Cedric has been trying to tell you until he's blue in the face, you NEED some sort of synchronisation (eg Critical Section) if you can open close these from different threads. Also, if you have multiple threads all able to close a handle, then your program is badly tangled already... Good luck, Iain.

                              Iain Clarke appearing by Special Request of CPallini.

                              E Offline
                              E Offline
                              Electronic75
                              wrote on last edited by
                              #19

                              I think I said in another post that this handle only is closed by one thread and after closing all other threads. Thanks for the effort Cedric ;) . I noticed a mistake of mine. while critical section is not necessary because never two threads use it synchronously but I noticed that mistakenly I terminate the thread and after that I try to close handle that has been created within that thread .No wonder! silly :-O! thanks all you guys, you are really helpful :)

                              1 Reply Last reply
                              0
                              • E Electronic75

                                I think I did not made my point clear, I said before using it. When one defines a pointer surely he wants to use it somewhere it is not null then and it should be checked especially if some naughty functions have played with it before. When one wanted to destroy the pointer then he can assign null to it as you said.

                                J Offline
                                J Offline
                                jhwurmbach
                                wrote on last edited by
                                #20

                                Electronic75 wrote:

                                When one defines a pointer [...] it is not null then

                                It should be. Always initialize your variables!

                                Electronic75 wrote:

                                t should be checked especially if some naughty functions have played with it before

                                Do not let any naughty functions touch your private parts! Do not program naughty functions in the first place. If naughty functions are made by someone else and keep doing horrible things to the objects you gave them pointers to, hand them a copy of the object, and keep the original with yourself.

                                Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                                Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                                E 1 Reply Last reply
                                0
                                • E Electronic75

                                  Good point but still before using it, it maybe necessary to check its validity. thanks

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

                                  Electronic75 wrote:

                                  ...it maybe necessary to check its validity.

                                  Which is what Cedric has been trying to tell you (by comparing the pointer to NULL).

                                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                                  "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

                                    Before running CloseHandle() I terminate those threads. Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space. I've used it so many times and I now I just forgot that and I can't find it in MSDN crap.

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

                                    Electronic75 wrote:

                                    Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space.

                                    AfxIsValidAddress() is a wrapper around IsBadReadPtr() and IsBadWritePtr().

                                    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

                                    1 Reply Last reply
                                    0
                                    • J jhwurmbach

                                      Electronic75 wrote:

                                      When one defines a pointer [...] it is not null then

                                      It should be. Always initialize your variables!

                                      Electronic75 wrote:

                                      t should be checked especially if some naughty functions have played with it before

                                      Do not let any naughty functions touch your private parts! Do not program naughty functions in the first place. If naughty functions are made by someone else and keep doing horrible things to the objects you gave them pointers to, hand them a copy of the object, and keep the original with yourself.

                                      Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                                      Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                                      E Offline
                                      E Offline
                                      Electronic75
                                      wrote on last edited by
                                      #23

                                      jhwurmbach wrote:

                                      It should be. Always initialize your variables!

                                      Of course I initialize pointers but not to NULL to some values.

                                      jhwurmbach wrote:

                                      Do not let any naughty functions touch your private parts!

                                      Well, I have no problem with girls :laugh: @ DavidCrow, I understood what cedric said but my point is when it has been given a value somewhere in the program and there is a possibility that value dose not exist anymore so the value is wrong then still I need to check it. Yes when I didn't need the pointer anymore I'll assign null to it and then compare it to null before using it.

                                      jhwurmbach wrote:

                                      Do not program naughty functions in the first place. If naughty functions are made by someone else and keep doing horrible things to the objects you gave them pointers to, hand them a copy of the object, and keep the original with yourself.

                                      Actually the naughty functions were also written by me but about 2-3 years ago and it is part of a scary and big project. Now I do not have enough time(as ever) and I have to add some additional functionalities before having time to inspect all the codes. I will check all the codes whenever I had time but now there is a big crowd who are ready to kill me if I do not give this software to them until the deadline(and the deadline already has been passed :~) but happily as I said in another post problem solved it was a silly mistake of mine :cool:

                                      J 1 Reply Last reply
                                      0
                                      • E Electronic75

                                        jhwurmbach wrote:

                                        It should be. Always initialize your variables!

                                        Of course I initialize pointers but not to NULL to some values.

                                        jhwurmbach wrote:

                                        Do not let any naughty functions touch your private parts!

                                        Well, I have no problem with girls :laugh: @ DavidCrow, I understood what cedric said but my point is when it has been given a value somewhere in the program and there is a possibility that value dose not exist anymore so the value is wrong then still I need to check it. Yes when I didn't need the pointer anymore I'll assign null to it and then compare it to null before using it.

                                        jhwurmbach wrote:

                                        Do not program naughty functions in the first place. If naughty functions are made by someone else and keep doing horrible things to the objects you gave them pointers to, hand them a copy of the object, and keep the original with yourself.

                                        Actually the naughty functions were also written by me but about 2-3 years ago and it is part of a scary and big project. Now I do not have enough time(as ever) and I have to add some additional functionalities before having time to inspect all the codes. I will check all the codes whenever I had time but now there is a big crowd who are ready to kill me if I do not give this software to them until the deadline(and the deadline already has been passed :~) but happily as I said in another post problem solved it was a silly mistake of mine :cool:

                                        J Offline
                                        J Offline
                                        jhwurmbach
                                        wrote on last edited by
                                        #24

                                        Electronic75 wrote:

                                        my point is when it has been given a value somewhere in the program and there is a possibility that value dose not exist anymore so the value is wrong then still I need to check it.

                                        Where does this possibility come from? THAT is the task you need to solve! Wherever you delete an object, you need to change the pointer value to NULL. Thats all, folks

                                        Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                                        Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                                        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