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. WaitForMultipleObjects getting access violation in Worker Thread

WaitForMultipleObjects getting access violation in Worker Thread

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpvisual-studiosecuritydebugging
18 Posts 5 Posters 22 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi In the process of re-writing my CAsyncSocket to the more controllable WSA flavor I got stuck on WaitForMultipleObjects. For Each Event on the socket that I do a bind by that I mean FD_READ, FD_WRITE I CreateEvent I associate the events with WSAEventSelect. I pass all the info (HWND windows message etc when I want notification to be sent in a structure) pointed to by a LPVOID the parameter in The CreateThread I let the worker thread wait on the events and do the appropriate SendMessage Problems is I am abending or rather getting an Access violation on WaitForMultipleobjects Running under the visual studio debugger I can see the values of the 5 events are correct The first thing i thought of was that my worker thread didnt have security rights to the events I created in the Mother thread ( all threads are in the same process) I just figured the quickest way to resolve this was to name the events and do a OpenEvent If I get a null from any events I would have a problem All of the events named from OpenEvent return a value however they are not the same event values that I created Real mystery to me Hope some one can help Thanks

    D V L 3 Replies Last reply
    0
    • F ForNow

      Hi In the process of re-writing my CAsyncSocket to the more controllable WSA flavor I got stuck on WaitForMultipleObjects. For Each Event on the socket that I do a bind by that I mean FD_READ, FD_WRITE I CreateEvent I associate the events with WSAEventSelect. I pass all the info (HWND windows message etc when I want notification to be sent in a structure) pointed to by a LPVOID the parameter in The CreateThread I let the worker thread wait on the events and do the appropriate SendMessage Problems is I am abending or rather getting an Access violation on WaitForMultipleobjects Running under the visual studio debugger I can see the values of the 5 events are correct The first thing i thought of was that my worker thread didnt have security rights to the events I created in the Mother thread ( all threads are in the same process) I just figured the quickest way to resolve this was to name the events and do a OpenEvent If I get a null from any events I would have a problem All of the events named from OpenEvent return a value however they are not the same event values that I created Real mystery to me Hope some one can help Thanks

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

      ForNow wrote:

      I let the worker thread wait on the events and do the appropriate SendMessage

      Should you be using PostMessage() instead?

      "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

      F 1 Reply Last reply
      0
      • D David Crow

        ForNow wrote:

        I let the worker thread wait on the events and do the appropriate SendMessage

        Should you be using PostMessage() instead?

        "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

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        You mean like this the worker thread doesn’t have to wait for a reply from send message Ok thanks

        1 Reply Last reply
        0
        • F ForNow

          Hi In the process of re-writing my CAsyncSocket to the more controllable WSA flavor I got stuck on WaitForMultipleObjects. For Each Event on the socket that I do a bind by that I mean FD_READ, FD_WRITE I CreateEvent I associate the events with WSAEventSelect. I pass all the info (HWND windows message etc when I want notification to be sent in a structure) pointed to by a LPVOID the parameter in The CreateThread I let the worker thread wait on the events and do the appropriate SendMessage Problems is I am abending or rather getting an Access violation on WaitForMultipleobjects Running under the visual studio debugger I can see the values of the 5 events are correct The first thing i thought of was that my worker thread didnt have security rights to the events I created in the Mother thread ( all threads are in the same process) I just figured the quickest way to resolve this was to name the events and do a OpenEvent If I get a null from any events I would have a problem All of the events named from OpenEvent return a value however they are not the same event values that I created Real mystery to me Hope some one can help Thanks

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          Do you pass any MFC object to the worker thread?

          F 1 Reply Last reply
          0
          • V Victor Nijegorodov

            Do you pass any MFC object to the worker thread?

            F Offline
            F Offline
            ForNow
            wrote on last edited by
            #5

            No not sure how WSAWaitForMultipleEvents is implemented in Windows MFC app But it was always my understanding based on feedback from people such as yourself that any kernel object wait I.E WaitForSingle/Multiple should be done in a worker thread correct ?

            V 1 Reply Last reply
            0
            • F ForNow

              No not sure how WSAWaitForMultipleEvents is implemented in Windows MFC app But it was always my understanding based on feedback from people such as yourself that any kernel object wait I.E WaitForSingle/Multiple should be done in a worker thread correct ?

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              ForNow wrote:

              No not sure how WSAWaitForMultipleEvents is implemented in Windows MFC app But it was always my understanding based on feedback from people such as yourself that any kernel object wait I.E WaitForSingle/Multiple should be done in a worker thread correct ?

              WSAWaitForMultipleEvents has nothing to do with MFC (note that it is defined in Winsock2.h header!). And what does it have to do with your using WaitForMultipleObjects function? :confused:

              F 1 Reply Last reply
              0
              • V Victor Nijegorodov

                ForNow wrote:

                No not sure how WSAWaitForMultipleEvents is implemented in Windows MFC app But it was always my understanding based on feedback from people such as yourself that any kernel object wait I.E WaitForSingle/Multiple should be done in a worker thread correct ?

                WSAWaitForMultipleEvents has nothing to do with MFC (note that it is defined in Winsock2.h header!). And what does it have to do with your using WaitForMultipleObjects function? :confused:

                F Offline
                F Offline
                ForNow
                wrote on last edited by
                #7

                I am using WaitForMultipleObjects rather thanWSAWaitForMultipleEvents for socket notification regardless don’t think the wait should be in the main thread

                V 1 Reply Last reply
                0
                • F ForNow

                  I am using WaitForMultipleObjects rather thanWSAWaitForMultipleEvents for socket notification regardless don’t think the wait should be in the main thread

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #8

                  ForNow wrote:

                  don’t think the wait should be in the main thread

                  If you would implement the Wait... in the main thread then you could wait forever and your main thread could be frozen forever. BTW, how do you create/obtain the event HANDLEs that you pass in the WaitForMultipleObjects? Are they all correct?

                  F 1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    ForNow wrote:

                    don’t think the wait should be in the main thread

                    If you would implement the Wait... in the main thread then you could wait forever and your main thread could be frozen forever. BTW, how do you create/obtain the event HANDLEs that you pass in the WaitForMultipleObjects? Are they all correct?

                    F Offline
                    F Offline
                    ForNow
                    wrote on last edited by
                    #9

                    I am using CreateEvent as it gives me more flexibility i.e auto reset As opposed to WSACreateEvent I then pass everything in a structure pointed to by LPVOID had to build it with no alignment (Zp1) as I was getting slack bytes among members of the structure thanks

                    V 1 Reply Last reply
                    0
                    • F ForNow

                      I am using CreateEvent as it gives me more flexibility i.e auto reset As opposed to WSACreateEvent I then pass everything in a structure pointed to by LPVOID had to build it with no alignment (Zp1) as I was getting slack bytes among members of the structure thanks

                      V Offline
                      V Offline
                      Victor Nijegorodov
                      wrote on last edited by
                      #10

                      Could you post some code snippets describing what you do?

                      F 1 Reply Last reply
                      0
                      • V Victor Nijegorodov

                        Could you post some code snippets describing what you do?

                        F Offline
                        F Offline
                        ForNow
                        wrote on last edited by
                        #11

                        sure be glad to in fact I appreciate it Am at work now (I work as a z/os Mainframe assemnbler programmer ) this has given me a chance to enhance my skills its 10:50 am here EST get off of work 5:00 - 5;30 will do it then thanks for your help

                        1 Reply Last reply
                        0
                        • F ForNow

                          Hi In the process of re-writing my CAsyncSocket to the more controllable WSA flavor I got stuck on WaitForMultipleObjects. For Each Event on the socket that I do a bind by that I mean FD_READ, FD_WRITE I CreateEvent I associate the events with WSAEventSelect. I pass all the info (HWND windows message etc when I want notification to be sent in a structure) pointed to by a LPVOID the parameter in The CreateThread I let the worker thread wait on the events and do the appropriate SendMessage Problems is I am abending or rather getting an Access violation on WaitForMultipleobjects Running under the visual studio debugger I can see the values of the 5 events are correct The first thing i thought of was that my worker thread didnt have security rights to the events I created in the Mother thread ( all threads are in the same process) I just figured the quickest way to resolve this was to name the events and do a OpenEvent If I get a null from any events I would have a problem All of the events named from OpenEvent return a value however they are not the same event values that I created Real mystery to me Hope some one can help Thanks

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          ForNow wrote:

                          I pass all the info (HWND windows

                          The MFC framework does not always give you the *real* HWND to a window. As strange as this might sound, the MFC framework maintains two object maps. The 'real objects' and an internal 'temporary handle map'. MFC will sometimes give you a temporary CTempWnd instead. If you are passing the window handle of a MFC class then you need to use CWnd::GetSafeHwnd[^]. There are some rare occasions where GetSafeHwnd cannot give you the real window handle and will return NULL instead so you should probably check for that too. Not many people are using MFC these days. You should consider moving on. I can't think of much else that would cause WaitForMultipleObjects to throw an access violation. The only other thing I can think of... is a corrupt heap. Sounds like you are somehow passing garbage to WaitForMultipleObjects. Best Wishes, -David Delaune

                          F C 2 Replies Last reply
                          0
                          • L Lost User

                            ForNow wrote:

                            I pass all the info (HWND windows

                            The MFC framework does not always give you the *real* HWND to a window. As strange as this might sound, the MFC framework maintains two object maps. The 'real objects' and an internal 'temporary handle map'. MFC will sometimes give you a temporary CTempWnd instead. If you are passing the window handle of a MFC class then you need to use CWnd::GetSafeHwnd[^]. There are some rare occasions where GetSafeHwnd cannot give you the real window handle and will return NULL instead so you should probably check for that too. Not many people are using MFC these days. You should consider moving on. I can't think of much else that would cause WaitForMultipleObjects to throw an access violation. The only other thing I can think of... is a corrupt heap. Sounds like you are somehow passing garbage to WaitForMultipleObjects. Best Wishes, -David Delaune

                            F Offline
                            F Offline
                            ForNow
                            wrote on last edited by
                            #13

                            Thanks I had some other problems I got rid of the WaitForMultipleObjects and set all my notifications on one event and extracted the correct one with WSAENumnetwork that worked when you say not many people are using MFC do yo mean they coding Windows C API I found CAsynsockets to be a pain I couldn’t pass along notification to another window Thanks

                            V 1 Reply Last reply
                            0
                            • F ForNow

                              Thanks I had some other problems I got rid of the WaitForMultipleObjects and set all my notifications on one event and extracted the correct one with WSAENumnetwork that worked when you say not many people are using MFC do yo mean they coding Windows C API I found CAsynsockets to be a pain I couldn’t pass along notification to another window Thanks

                              V Offline
                              V Offline
                              Victor Nijegorodov
                              wrote on last edited by
                              #14

                              ForNow wrote:

                              when you say not many people are using MFC do yo mean they coding Windows C API I found CAsynsockets to be a pain I couldn’t pass along notification to another window

                              I guess he meant they use C#. I used CAsyncSocket class successfully since many years (until I have changed the company to work for). I never had any problems with this class to work with both IPv4 and IPv6 sockets in IPC with multiple sockets through intranet and internet.

                              F 1 Reply Last reply
                              0
                              • V Victor Nijegorodov

                                ForNow wrote:

                                when you say not many people are using MFC do yo mean they coding Windows C API I found CAsynsockets to be a pain I couldn’t pass along notification to another window

                                I guess he meant they use C#. I used CAsyncSocket class successfully since many years (until I have changed the company to work for). I never had any problems with this class to work with both IPv4 and IPv6 sockets in IPC with multiple sockets through intranet and internet.

                                F Offline
                                F Offline
                                ForNow
                                wrote on last edited by
                                #15

                                C/C++ has pointers which gives me flexibility same with native sockets “WSA” when I tried moving my derived CAsyncSocket object pointer notification from a main window to a dialog I ran into problems. No where in the documentation does it say there has to be HWND associated with your CasyncSockets but you have to I was using CWinThread for notification it has a message pump but doesn’t have a HWND although there is hWnd member ( I think main windows ) member thanks

                                V 1 Reply Last reply
                                0
                                • F ForNow

                                  C/C++ has pointers which gives me flexibility same with native sockets “WSA” when I tried moving my derived CAsyncSocket object pointer notification from a main window to a dialog I ran into problems. No where in the documentation does it say there has to be HWND associated with your CasyncSockets but you have to I was using CWinThread for notification it has a message pump but doesn’t have a HWND although there is hWnd member ( I think main windows ) member thanks

                                  V Offline
                                  V Offline
                                  Victor Nijegorodov
                                  wrote on last edited by
                                  #16

                                  It is very hard to understand what you mean, much more harder is to understand what, how, and what for you have implemented in your project. The reason - you avoid to show your code/pseudocode snippets. Sorry... :~

                                  F 1 Reply Last reply
                                  0
                                  • V Victor Nijegorodov

                                    It is very hard to understand what you mean, much more harder is to understand what, how, and what for you have implemented in your project. The reason - you avoid to show your code/pseudocode snippets. Sorry... :~

                                    F Offline
                                    F Offline
                                    ForNow
                                    wrote on last edited by
                                    #17

                                    I’ll do that tonite ( it’s 8:45 est by me ) wanted to fix up my code Thanks

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      ForNow wrote:

                                      I pass all the info (HWND windows

                                      The MFC framework does not always give you the *real* HWND to a window. As strange as this might sound, the MFC framework maintains two object maps. The 'real objects' and an internal 'temporary handle map'. MFC will sometimes give you a temporary CTempWnd instead. If you are passing the window handle of a MFC class then you need to use CWnd::GetSafeHwnd[^]. There are some rare occasions where GetSafeHwnd cannot give you the real window handle and will return NULL instead so you should probably check for that too. Not many people are using MFC these days. You should consider moving on. I can't think of much else that would cause WaitForMultipleObjects to throw an access violation. The only other thing I can think of... is a corrupt heap. Sounds like you are somehow passing garbage to WaitForMultipleObjects. Best Wishes, -David Delaune

                                      C Offline
                                      C Offline
                                      Christmas Fantasy
                                      wrote on last edited by
                                      #18

                                      I've managed to get SharpDX to play a video from a file without problems, and also have been able to do my 2D drawing. This was simple enough and the performance is good on Speed Test - TestMySpeed[^] .

                                      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