473,387 Members | 1,321 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,387 software developers and data experts.

Strange Corrupt Pointer - Microsoft Bug??

Hi,

I have written a simple program that does the following:

The main program will spown MAX_THREADS number of threads, each
of which will simply add to a global shared counter for MAX_COUNT
times and notify the main thread it has finished by clearing a
mutex before exiting. The main thread will simply check verify the
final result in the global shared counter, wait for each worker
thread has notify finishing, and exit.

The complete C program is at the end of this message. Anyway,
the problem is not in the logic of this program. It is really an
anomaly I encountered while testing it with different values of
MAX_THREADS.
I found that for some reason, if the MAX_THREADS value is 1 or 2 the
hRunMutex[0] will be corrupted during execution for no apparent
reason. The program will go into an infinite loop in CheckCounter()
function. I can detect this by checking if the return value of
WaitForSingleObject() function equals WAIT_FAILED, etc, but that's
another topic. So I was playing with the program and tried to
print out the value of hRunMutex[0] at various points and I found
that if I add on extra global variable definition, the whole problem
goes away! That is if I add "char func[10];".

For MAX_THREADS value 3 and above, the program works well with
or without that line. But for value 1 and 2, it has to have that line.
Otherwise it gets a currupt handle during execution and could not
finish.

I am really curious about the cause of this problem. I'd appreciate
if anyone can shed some light on it for me, because besides blaming
Microsoft, I am completely clueless. :)

And the reason I am blaming Microsoft is I am using Visual Studio
..NET to compile the program. Just create a project with Managed C++
Empty Project, and add this file to project to compile and run it.

Thanks!

Regards,
James

/* counters.c file.*/

/* This program will create MAX_THREADS number of threads. Each of
them
* will increment a shared counter for MAX_COUNT times, sleeping for a
* random number of milliseconds in between each increment. The main
* thread will wait until the shared counter reaches
MAX_THREADS*MAX_COUNT.
* It will then wait for enter key from input before exiting to allow
* examining the output.
*/

#include <windows.h>
#include <stdio.h>
#include <process.h>

#define MAX_THREADS 2
#define MAX_COUNT 3

/* getrandom returns a random number between min and max. */
#define getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) +
(min))

int main( void ); /* Thread 1: main */
void CountProc( int * MyID ); /* Threads 2 to MAX_THREADS:
Increment the shared counter */
void CheckCounter( void ); /* Function CheckCounter called
by main() */
void WriteMsg( int iThreadNum, int iMyCount, int iTotalCount ); /*
Display information */

HANDLE hRunMutex[MAX_THREADS]; /* Notification mutex */
int iThreadNum; /* Number of threads started */
HANDLE hCounterMutex; /* Shared counter mutex */
int iSharedCounter; /* Shared counter used by all threads
*/
int ThreadID[MAX_THREADS]; /* Thread ID for diaplay */
int done[MAX_THREADS] = {0}; /* Array for thread status */

char func[10]; // Necessary to make it work for MAX_THREADS = 1
and 2 cases. Microsoft bug?!

int main() /* Thread One */
{
/* Create the mutexes and reset thread count. */
int i;
iSharedCounter = 0;
for(i=0;i< MAX_THREADS;i++)
{
hRunMutex[i] = CreateMutex( NULL, TRUE, NULL ); /* Set */
}
hCounterMutex = CreateMutex( NULL, FALSE, NULL ); /* Cleared */
iThreadNum = 0;

WriteMsg( 0, 0, iSharedCounter );

/* Create the counting threads. */
while( iThreadNum < MAX_THREADS )
{
iThreadNum++;
ThreadID[iThreadNum] = iThreadNum;
_beginthread( CountProc, 0, &ThreadID[iThreadNum] );
}

/* Wait until the shared counter reaches the limit. */
CheckCounter();
WriteMsg( 0, 0, iSharedCounter );

/* All threads done. Clean up handles. */
for(i=0;i<MAX_THREADS;i++)
{
/* if(done[i] != 1) */
CloseHandle( hRunMutex[i] );
}
CloseHandle( hCounterMutex);

/* Pause for 10 seconds before exiting */
printf("Press enter to exit...");
getchar();
}

void CheckCounter( void ) /* Check shared counter */
{
int i;

/* Check the share counter limit, sleep in between each check */
while ( iSharedCounter < MAX_COUNT * MAX_THREADS)
{
Sleep(getrandom(0,100));
}

/* Wait for the woker threads to exit first */
while ( iThreadNum > 0 )
{
for(i = 0;i<MAX_THREADS;i++)
{
if(done[i] == 0 )
{
int ret = WaitForSingleObject( hRunMutex[i], 100 );
if(ret == WAIT_OBJECT_0 /*|| ret == WAIT_FAILED*/)
{
iThreadNum--;
done[i]=ret+2;
}
}
}
Sleep(getrandom(0,100));
}
}

void CountProc( int *MyID )
{
int i=0;
do
{
/* Wait for counter to be available, then lock it. */
WaitForSingleObject( hCounterMutex, INFINITE );
iSharedCounter++;
WriteMsg(*MyID, i, iSharedCounter);
ReleaseMutex( hCounterMutex );

i++;
Sleep(getrandom(0, 5));
}
while(i < MAX_COUNT);

ReleaseMutex( hRunMutex[(*MyID)-1] );
}

void WriteMsg( int iThreadNum, int iMyCount, int iTotalCount )
{
printf("Thread ID: %d, Local Count: %d, Shared Count: %d \n",
iThreadNum, iMyCount, iTotalCount );
}

Jul 22 '05 #1
2 1785
"James Niceguy" <or*****@yahoo.com> wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
Hi,

I have written a simple program that does the following:

The main program will spown MAX_THREADS number of threads, each
Off-topic.... Standard C++ says nothing about Threads.
of which will simply add to a global shared counter for MAX_COUNT
times and notify the main thread it has finished by clearing a
mutex before exiting. The main thread will simply check verify the
Off-topic.... Standard C++ says nothing about Mutexes.
final result in the global shared counter, wait for each worker
thread has notify finishing, and exit.

The complete C program is at the end of this message. Anyway,
the problem is not in the logic of this program. It is really an
anomaly I encountered while testing it with different values of
MAX_THREADS.
I found that for some reason, if the MAX_THREADS value is 1 or 2 the
hRunMutex[0] will be corrupted during execution for no apparent
reason. The program will go into an infinite loop in CheckCounter()
function. I can detect this by checking if the return value of
WaitForSingleObject() function equals WAIT_FAILED, etc, but that's
another topic. So I was playing with the program and tried to
print out the value of hRunMutex[0] at various points and I found
that if I add on extra global variable definition, the whole problem
goes away! That is if I add "char func[10];".

For MAX_THREADS value 3 and above, the program works well with
or without that line. But for value 1 and 2, it has to have that line.
Otherwise it gets a currupt handle during execution and could not
finish.

I am really curious about the cause of this problem. I'd appreciate
if anyone can shed some light on it for me, because besides blaming
Microsoft, I am completely clueless. :)

And the reason I am blaming Microsoft is I am using Visual Studio
.NET to compile the program. Just create a project with Managed C++
Empty Project, and add this file to project to compile and run it.

Thanks!

Regards,
James

/* counters.c file.*/

/* This program will create MAX_THREADS number of threads. Each of
them
* will increment a shared counter for MAX_COUNT times, sleeping for a
* random number of milliseconds in between each increment. The main
* thread will wait until the shared counter reaches
MAX_THREADS*MAX_COUNT.
* It will then wait for enter key from input before exiting to allow
* examining the output.
*/

#include <windows.h>
#include <stdio.h>
#include <process.h>

#define MAX_THREADS 2
#define MAX_COUNT 3

/* getrandom returns a random number between min and max. */
#define getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) +
(min))

int main( void ); /* Thread 1: main */
void CountProc( int * MyID ); /* Threads 2 to MAX_THREADS:
Increment the shared counter */
void CheckCounter( void ); /* Function CheckCounter called
by main() */
void WriteMsg( int iThreadNum, int iMyCount, int iTotalCount );
/* Display information */

HANDLE hRunMutex[MAX_THREADS]; /* Notification mutex */
int iThreadNum; /* Number of threads started */
HANDLE hCounterMutex; /* Shared counter mutex */
int iSharedCounter; /* Shared counter
used by all threads */
int ThreadID[MAX_THREADS]; /* Thread ID for diaplay */
int done[MAX_THREADS] = {0}; /* Array for thread status */

char func[10]; // Necessary to make it work for MAX_THREADS = 1
and 2 cases. Microsoft bug?!

int main() /* Thread One */
{
/* Create the mutexes and reset thread count. */
int i;
iSharedCounter = 0;
for(i=0;i< MAX_THREADS;i++)
{
hRunMutex[i] = CreateMutex( NULL, TRUE, NULL ); /* Set */
}
hCounterMutex = CreateMutex( NULL, FALSE, NULL ); /* Cleared */
iThreadNum = 0;

WriteMsg( 0, 0, iSharedCounter );

/* Create the counting threads. */
while( iThreadNum < MAX_THREADS )
{
iThreadNum++;
ThreadID[iThreadNum] = iThreadNum;
Undefined behaviour. The second time through this loop, you increment
iThreadNum (making it 2) and then attempted to write into an array beyond
its bounds (ThreadID, only has elements 0 and 1).
_beginthread( CountProc, 0, &ThreadID[iThreadNum] );
}

/* Wait until the shared counter reaches the limit. */
CheckCounter();
WriteMsg( 0, 0, iSharedCounter );

/* All threads done. Clean up handles. */
for(i=0;i<MAX_THREADS;i++)
{
/* if(done[i] != 1) */
CloseHandle( hRunMutex[i] );
}
CloseHandle( hCounterMutex);

/* Pause for 10 seconds before exiting */
printf("Press enter to exit...");
getchar();
}

void CheckCounter( void ) /* Check shared counter */
{
int i;

/* Check the share counter limit, sleep in between each check */
while ( iSharedCounter < MAX_COUNT * MAX_THREADS)
{
Sleep(getrandom(0,100));
}

/* Wait for the woker threads to exit first */
while ( iThreadNum > 0 )
{
for(i = 0;i<MAX_THREADS;i++)
{
if(done[i] == 0 )
{
int ret = WaitForSingleObject( hRunMutex[i], 100 );
if(ret == WAIT_OBJECT_0 /*|| ret == WAIT_FAILED*/)
{
iThreadNum--;
done[i]=ret+2;
}
}
}
Sleep(getrandom(0,100));
}
}

void CountProc( int *MyID )
{
int i=0;
do
{
/* Wait for counter to be available, then lock it. */
WaitForSingleObject( hCounterMutex, INFINITE );
iSharedCounter++;
WriteMsg(*MyID, i, iSharedCounter);
ReleaseMutex( hCounterMutex );

i++;
Sleep(getrandom(0, 5));
}
while(i < MAX_COUNT);

ReleaseMutex( hRunMutex[(*MyID)-1] );
}

void WriteMsg( int iThreadNum, int iMyCount, int iTotalCount )
{
printf("Thread ID: %d, Local Count: %d, Shared Count: %d \n",
iThreadNum, iMyCount, iTotalCount );
}


Jul 22 '05 #2
Hi Andre,

Thank you for the answer! You are right on!! I changed the line to:

ThreadID[iThreadNum-1] = iThreadNum;

and the program works like a charm. Sorry if the policy is not to
discuss off-topic issues here. I will read the rules next time. Guess
I shouldn't be blaming MS so readily!

Regards,
James
Andre Kostur wrote:
"James Niceguy" <or*****@yahoo.com> wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
Hi,

I have written a simple program that does the following:

The main program will spown MAX_THREADS number of threads, each
Off-topic.... Standard C++ says nothing about Threads.
of which will simply add to a global shared counter for MAX_COUNT
times and notify the main thread it has finished by clearing a
mutex before exiting. The main thread will simply check verify the


Off-topic.... Standard C++ says nothing about Mutexes.
final result in the global shared counter, wait for each worker
thread has notify finishing, and exit.

The complete C program is at the end of this message. Anyway,
the problem is not in the logic of this program. It is really an
anomaly I encountered while testing it with different values of
MAX_THREADS.
I found that for some reason, if the MAX_THREADS value is 1 or 2 the hRunMutex[0] will be corrupted during execution for no apparent
reason. The program will go into an infinite loop in CheckCounter()
function. I can detect this by checking if the return value of
WaitForSingleObject() function equals WAIT_FAILED, etc, but that's
another topic. So I was playing with the program and tried to
print out the value of hRunMutex[0] at various points and I found
that if I add on extra global variable definition, the whole problem goes away! That is if I add "char func[10];".

For MAX_THREADS value 3 and above, the program works well with
or without that line. But for value 1 and 2, it has to have that line. Otherwise it gets a currupt handle during execution and could not
finish.

I am really curious about the cause of this problem. I'd appreciate
if anyone can shed some light on it for me, because besides blaming
Microsoft, I am completely clueless. :)

And the reason I am blaming Microsoft is I am using Visual Studio
.NET to compile the program. Just create a project with Managed C++
Empty Project, and add this file to project to compile and run it.

Thanks!

Regards,
James

/* counters.c file.*/

/* This program will create MAX_THREADS number of threads. Each of
them
* will increment a shared counter for MAX_COUNT times, sleeping for a * random number of milliseconds in between each increment. The main * thread will wait until the shared counter reaches
MAX_THREADS*MAX_COUNT.
* It will then wait for enter key from input before exiting to allow * examining the output.
*/

#include <windows.h>
#include <stdio.h>
#include <process.h>

#define MAX_THREADS 2
#define MAX_COUNT 3

/* getrandom returns a random number between min and max. */
#define getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))

int main( void ); /* Thread 1: main */
void CountProc( int * MyID ); /* Threads 2 to MAX_THREADS:
Increment the shared counter */
void CheckCounter( void ); /* Function CheckCounter called by main() */
void WriteMsg( int iThreadNum, int iMyCount, int iTotalCount ); /* Display information */

HANDLE hRunMutex[MAX_THREADS]; /* Notification mutex */
int iThreadNum; /* Number of threads started */ HANDLE hCounterMutex; /* Shared counter mutex */
int iSharedCounter; /* Shared counter
used by all threads */
int ThreadID[MAX_THREADS]; /* Thread ID for diaplay */
int done[MAX_THREADS] = {0}; /* Array for thread status */
char func[10]; // Necessary to make it work for MAX_THREADS = 1 and 2 cases. Microsoft bug?!

int main() /* Thread One */
{
/* Create the mutexes and reset thread count. */
int i;
iSharedCounter = 0;
for(i=0;i< MAX_THREADS;i++)
{
hRunMutex[i] = CreateMutex( NULL, TRUE, NULL ); /* Set */
}
hCounterMutex = CreateMutex( NULL, FALSE, NULL ); /* Cleared */
iThreadNum = 0;

WriteMsg( 0, 0, iSharedCounter );

/* Create the counting threads. */
while( iThreadNum < MAX_THREADS )
{
iThreadNum++;
ThreadID[iThreadNum] = iThreadNum;


Undefined behaviour. The second time through this loop, you

increment iThreadNum (making it 2) and then attempted to write into an array beyond its bounds (ThreadID, only has elements 0 and 1).
_beginthread( CountProc, 0, &ThreadID[iThreadNum] );
}

/* Wait until the shared counter reaches the limit. */
CheckCounter();
WriteMsg( 0, 0, iSharedCounter );

/* All threads done. Clean up handles. */
for(i=0;i<MAX_THREADS;i++)
{
/* if(done[i] != 1) */
CloseHandle( hRunMutex[i] );
}
CloseHandle( hCounterMutex);

/* Pause for 10 seconds before exiting */
printf("Press enter to exit...");
getchar();
}

void CheckCounter( void ) /* Check shared counter */ {
int i;

/* Check the share counter limit, sleep in between each check */
while ( iSharedCounter < MAX_COUNT * MAX_THREADS)
{
Sleep(getrandom(0,100));
}

/* Wait for the woker threads to exit first */
while ( iThreadNum > 0 )
{
for(i = 0;i<MAX_THREADS;i++)
{
if(done[i] == 0 )
{
int ret = WaitForSingleObject( hRunMutex[i], 100 );
if(ret == WAIT_OBJECT_0 /*|| ret == WAIT_FAILED*/)
{
iThreadNum--;
done[i]=ret+2;
}
}
}
Sleep(getrandom(0,100));
}
}

void CountProc( int *MyID )
{
int i=0;
do
{
/* Wait for counter to be available, then lock it. */
WaitForSingleObject( hCounterMutex, INFINITE );
iSharedCounter++;
WriteMsg(*MyID, i, iSharedCounter);
ReleaseMutex( hCounterMutex );

i++;
Sleep(getrandom(0, 5));
}
while(i < MAX_COUNT);

ReleaseMutex( hRunMutex[(*MyID)-1] );
}

void WriteMsg( int iThreadNum, int iMyCount, int iTotalCount )
{
printf("Thread ID: %d, Local Count: %d, Shared Count: %d \n",
iThreadNum, iMyCount, iTotalCount );
}


Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vinay | last post by:
Hi I have a corrupt word file. I am able to open it with the code given below tr Dim pInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo( pInfo.UseShellExecute...
3
by: Jeffrey Baker | last post by:
Hi, I am recompiling a program from VC++ 5.0 to VC++.NET interface. C++ Compliance is tighter. I get the error message before the program finished that stack corrupt around "obj". - this being...
4
by: Eric E | last post by:
Hi all, I have a fairly complex form in Access 2000. In particular, it has two subforms on separate tabs of a tab control. For the last two weeks, I've encountered the dreaded : "You can't...
2
by: Joseph Macari | last post by:
I recently installed Office2003 on my computer. I had imported (not linked) a couple of tables from an Access 2000mdb into an Access 2003mdb. I had composed various queries and forms with these...
1
by: Default | last post by:
Hi, I am new to C#, that is why I am not sure what kind of problem it is: Is VS files corrupted , or something else. that is the problems description: I am working on a small database project. I am...
2
by: Mongoose7 | last post by:
Hi, I am using vc7 to call a dll function from another dll. The function seems to execute correctly (it writes binary data to the registry) but when it comes out of the function, and tries to...
2
by: WoodenSWord | last post by:
Hello i would like to share with you my adventure!!! IIS could not load aspx or asmx pages by NO Means! I reinstalled/unistalled/installed IIS .NET FRAMEWORK 1.1 & 2 , Vs2005 ,Sql server 2005...
5
by: Jeff | last post by:
Okay, I'm still new to vb.net 2005 - throught this was a hardware problem, but now I don't know. (I'm having some problem with my newgroup provider, so hopefully this will go through) This...
2
by: danep2 | last post by:
Hello all This is a really strange problem. I have code that performs a few calculations based on input from a joystick, and writes these values to a file using basically the following code: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.