Usually I like to solve the exceptionissue, because it always caused by processing the same resource by differentthreads.
Today, also there is an exception issue,and it is a data abort issue exactly, but it is not so lucky for me this time.
The exception raised when my programprocess the error handle, yes, it is easy to find the root cause. I onlysimulate the case to let the program return false directly at somewhere. Ithink my lucky is still so good till now, I got the root cause, because thereis some error and return, so some function not implement, and some resource notalloc or initialize. But when processing the error handle, we free anddeinitialize the resource as it has been normally alloced and initialized. Sowe meet the data abort exception.
OK, the issue is solved, you are lucky. Butyou will not think so, if I tell you the error is CreateThread return NULL.
It is so strange. Why does the CreateThreadreturn NULL? By the way, it not return null each time, but after it was calledmany time, then return null.
First thinking, no enough memory. Yes, thisis the only reason I think. But after I check the free memory, it is verylarge. OK, I have no idea about that, let’s check the MSDN:
The number of threads a process can create is limited bythe available virtual memory and depends on the default stack size.
If every thread has 1 MB of stack space, you can create amaximum of 32 threads.
If you reduce the default stack size, you can create morethreads.
Maybethere is no enough memory for thread stack to create thread in the process? Godtold me that is not the reason, another thing raise a lighting in my brain, thefunction CreateFileMapping also return null, and both CreateFileMapping andCreateThread are turn the type of HANLDE. As I remember, the HANDLE in OS of MSis a limited resource. If it is exhaust, the two function should return null.
Itis the root cause in my mind. Now what should I do is to verify this is theroot cause. Let’s create event but not close crazily.
Nodoubting, the test result show me that the CreateEvent always return NULL at aregualer time. At the same timeIobserved there is following info in MSDN:
Thethread object remains in the system until the thread has terminated and allhandles to it are closed through a call to CloseHandle.
My god, I miss theCloseHandle in my fucking code. But as I remember I intentional miss that, because I think the temperory variable of theHANDLE will be closed when the function return. Obviously I make a mistake.
En, do not care whichresource cause the fail, CloseHandle should be called after you CreateThread.By the way, if you do not using the HANDLE to do any other thing, you canCloseHandle it following the CreateThread.
Thank my God, if customer find the issue,my boss will kill me.
转载于:https://blog.51cto.com/bluefish/1347293