470,871 Members | 1,930 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,871 developers. It's quick & easy.

access violation reading location when using WaitForMultipleObjects


I am writing a multi-threads encryption application. The idea of the code is to create a number of threads to encrypt files. I have a thread pool say the maximum threads is 10. If the number of tasks (number of files waiting to be encrypted) is larger than the thread pool size, I first create 10 new threads to execute the encryption function. Then I wait for any of the threads to be signaled in the thread pool using WaitForMultipleObjects with WaitAll bit set to false. Then I close the handle to that thread and create a new thread on the slot. After all tasks are done, I call WaitForMultipleObjects with WaitAll bit to TRUE and wait all threads to terminate.

The error, access violation reading location 0x00000000 occurs in the last step. Trying to debug it, the memcpy.asm code pops up and terminates at the last line: rep movsd

Expand|Select|Wrap|Line Numbers
  1. Dword_align:
  2.         test    edi,11b         ;U - destination dword aligned?
  3.         jnz     short CopyLeadUp ;V - if we are not dword aligned already, align
  5.         shr     ecx,2           ;U - shift down to dword count
  6.         and     edx,11b         ;V - trailing byte count
  8.         cmp     ecx,8           ;U - test if small enough for unwind copy
  9.         jb      short CopyUnwindUp ;V - if so, then jump
  11.         rep     movsd           ;N - move all of our dwords
I tried to debug it and found out a very strange thing. When the executed task jumps over the 10th, a WaitForMultipleObjects(with waitall = false) is called. I can therefore find out which thread got signaled (for example thread number 5). Then a new thread is created for the task number 11. I can see the thread ID of this newly created thread is different from the first 5th threads, but the return value of CreateThread which is the handle of this thread is still equal to the handle of the 5th thread.

Nov 14 '07 #1
3 7698
Sorry. I found out the error when studying the call stack. It has nothing to do with the multi-thread programming but because the key used in the encryption function is destroyed when any of the encryption function is finished within a thread. The memory access violation was caused by reading an empty encryption key:-)

Nov 14 '07 #2
4,677 Expert Mod 4TB
Well, thanks for posting anyway, please feel free to post again if you run into anything else.
Nov 14 '07 #3
I run to the same error because i send to WaitForMultipleObjects an handle pointer not an static array. Remember max processes over XP is 64 process.
See the following example, it works perfectly:

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <process.h>
  8. // Secound Thread function
  9. void ThreadProc(void *param);
  12. // First thread 
  13. int main()
  14. {
  16.     int n;
  17.     int i;
  18.     int val = 0;
  19.     HANDLE handle;
  20.     DWORD dwRetVal ;    
  22.     printf("\t Thread Demo\n");
  23.     printf("Enter the number of threads : ");
  24.     scanf("%d",&n);
  26.     while (n>MAXIMUM_WAIT_OBJECTS)
  27.     {
  28.         printf("Enter the number of threads smaller than 64 : ");
  29.         scanf("%d",&n);
  30.     }
  31.         {
  32.         //HANDLE *pArrayInt= (HANDLE *)malloc (n);
  33.         HANDLE pArrayInt[MAXIMUM_WAIT_OBJECTS];
  35.         for(i=0;i<n;i++)
  36.         {
  37.             val = i+1;
  38.             handle = (HANDLE) _beginthread( ThreadProc,0,&val); // create thread
  39.             pArrayInt[i] = handle;
  40.             printf ("%d Handle \n",handle);
  41.         }
  42.         dwRetVal =WaitForMultipleObjects((DWORD)n,pArrayInt,TRUE,INFINITE);
  43.     } 
  44.     return 0;
  45. }
  48. void ThreadProc(void *param)
  49. {
  51.     char *program = "c:\\WINDOWS\\notepad.exe";
  52.     spawnl(P_WAIT,program,program,NULL);
  53. }
Feb 15 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Microsoft News | last post: by
4 posts views Thread by Matt Sawyer | last post: by
1 post views Thread by Nasser | last post: by
reply views Thread by Bruce Pataki | last post: by
10 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.