473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

access violation reading location when using WaitForMultipleObjects

4
Hi,

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
  4.  
  5.         shr     ecx,2           ;U - shift down to dword count
  6.         and     edx,11b         ;V - trailing byte count
  7.  
  8.         cmp     ecx,8           ;U - test if small enough for unwind copy
  9.         jb      short CopyUnwindUp ;V - if so, then jump
  10.  
  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.

Thanks
Nov 14 '07 #1
3 8010
Pinux
4
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:-)

cheers
Nov 14 '07 #2
sicarie
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
nawito
2
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>
  7.  
  8. // Secound Thread function
  9. void ThreadProc(void *param);
  10.  
  11.  
  12. // First thread 
  13. int main()
  14. {
  15.  
  16.     int n;
  17.     int i;
  18.     int val = 0;
  19.     HANDLE handle;
  20.     DWORD dwRetVal ;    
  21.  
  22.     printf("\t Thread Demo\n");
  23.     printf("Enter the number of threads : ");
  24.     scanf("%d",&n);
  25.  
  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];
  34.  
  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. }
  46.  
  47.  
  48. void ThreadProc(void *param)
  49. {
  50.  
  51.     char *program = "c:\\WINDOWS\\notepad.exe";
  52.     spawnl(P_WAIT,program,program,NULL);
  53. }
Feb 15 '08 #4

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

Similar topics

0
by: Microsoft News | last post by:
I'm getting the following error when I shut down my C# .NET v1.1 application: 0xC0000005: Access violation reading location 0x73bc0000 This error didn't occur until I added a...
4
by: Matt Sawyer | last post by:
I am attempting to use an API (CxApiOem.dll) that has a large number of defines and complicated structs. It's just too much hassle to attempt to use DLLImport to make the desired API calls. ...
3
by: Binary | last post by:
VC++ .NET 2003: Access violation with /O2 compiler switch; no AV with /O Hi I'm in the process of narrowing down a problem, and I have reduced the code involved to the following If someone could...
2
by: Boris Fortes | last post by:
I need to unhook event receiver as result of native C++ event. It unhooks successfully, but __raise does not return and throws access violation. Visual Studio 2003 How to reproduce: Consol...
1
by: Nasser | last post by:
Hello, I am coding a mathematical model with VC++. When I debug the code, I face with no erroe, but during executing, I face with below error "Unhandled exception at 0x0040c275 in Tar.exe:...
0
by: Bruce Pataki | last post by:
I am creating an MFC application with activeX document server support. The application runs perfectly fine when i run as a standalone application. But when i run the application in Internet...
2
by: bhreddy | last post by:
Hi All, Can someone help me out how can I resolve the error "0xC0000005: Access violation reading location 0x513112f4"? Steps I followed... 1. I ran the application at DOS prompt 2. After...
10
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, There is error message when executing my program, Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access violation reading location 0x00000002. It is...
13
by: ycinar | last post by:
A quick question: Why doesn't the following code catch the Access violation reading location exception? it crashes on line if ( xyz ) with an Access violation reading location exception. Is not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.