mike_marquet
asked on
CreateThread Problem !
Hi to all,
I have a little problem after x hours my application is running. In my application, I use 'CreateThread' to start a worker thread but after x hours, the function returns me NULL and GetLastError() returns me 8 (Not enough storage is available to process this command).
Can someone help me to find from where the problems comes from.
I have a little problem after x hours my application is running. In my application, I use 'CreateThread' to start a worker thread but after x hours, the function returns me NULL and GetLastError() returns me 8 (Not enough storage is available to process this command).
Can someone help me to find from where the problems comes from.
At least you have to show some code. Looks like you have somewhere memory leak
Yes, definitly there is memory leak. What all u r doing in thread procs? R U allocating memory and not deleting it? R U handling some GDI objects and not deleting them once again? Post some code here, so that we can make out what is happening.
VinExpert
VinExpert
ASKER
Here's the code where the problem occurs. I have checked it with boundchecker which says me that there is a resource leak that comes from CreateThread but I call CloseHandle atfer it.
This thread is used to simulate DCOM timeout. If the DCOM call doesn't response in the specified timeout, the thread is killed.
BOOL CDCOMClassWithDelay::CallT hreadAndWa it(THREAD_ INFO *pstInfo)
{
m_bTimeoutError = FALSE;
DWORD dwThreadId;
// Call thread and start DCOM call
m_hThread = ::CreateThread(NULL,
0,
DCOMClassWithDelayThread,
pstInfo,
0,
&dwThreadId);
if (!m_hThread)
{
m_strErrorMessage.Format(" Failed to start DCOM Delay thread [error code = %lu]", GetLastError());
return FALSE;
}
SetThreadPriority(m_hThrea d, m_iThreadPriority);
// Wait x milliseconds for timeout
DWORD dwRet = WaitForSingleObject(m_hThr ead, m_dwDelay);
if (dwRet == WAIT_OBJECT_0) // DCOM call successfull
{
CloseHandle(m_hThread); // Free resource
m_hThread = NULL;
return TRUE;
}
// DCOM call timeout, kill thread
DWORD dwExitCode;
if (!GetExitCodeThread(m_hThr ead,&dwExi tCode) || dwExitCode == STILL_ACTIVE)
{
BOOL bRet = TerminateThread(m_hThread, 99);
if (!bRet) AddLogLine("CDCOMClassWith Delay - TerminateThread failed with error %lu", LOGBOOK_WARNING, GetLastError());
}
CloseHandle(m_hThread); // Free resource
m_hThread = NULL;
m_strErrorMessage.Format(" DCOM Delay timeout");
m_bTimeoutError = TRUE;
return FALSE; // Timeout
}
This thread is used to simulate DCOM timeout. If the DCOM call doesn't response in the specified timeout, the thread is killed.
BOOL CDCOMClassWithDelay::CallT
{
m_bTimeoutError = FALSE;
DWORD dwThreadId;
// Call thread and start DCOM call
m_hThread = ::CreateThread(NULL,
0,
DCOMClassWithDelayThread,
pstInfo,
0,
&dwThreadId);
if (!m_hThread)
{
m_strErrorMessage.Format("
return FALSE;
}
SetThreadPriority(m_hThrea
// Wait x milliseconds for timeout
DWORD dwRet = WaitForSingleObject(m_hThr
if (dwRet == WAIT_OBJECT_0) // DCOM call successfull
{
CloseHandle(m_hThread); // Free resource
m_hThread = NULL;
return TRUE;
}
// DCOM call timeout, kill thread
DWORD dwExitCode;
if (!GetExitCodeThread(m_hThr
{
BOOL bRet = TerminateThread(m_hThread,
if (!bRet) AddLogLine("CDCOMClassWith
}
CloseHandle(m_hThread); // Free resource
m_hThread = NULL;
m_strErrorMessage.Format("
m_bTimeoutError = TRUE;
return FALSE; // Timeout
}
It's really really not recomended to use TerminateThread. Can you use some other technique for stoppint it?
Hi,
U change the code to do something like this and see...
1. Have the event handles created in ur application.
2. In the function instead of CreateThread use AfxBeginThread function to launch the thread to simulate the wait. Before that, set the events to reset state by ResetEvent(...) call
3. Then use WaitForMultipleObject(...) to wait on the event handles.
4. In the simulator thread, if successful, set one event and if fail set another and use AfxEndThread() to terminate the thread.
5. In the function, now the WaitForMultipleObject will return with the successful or fail or timeout events. Just check that.
This will make lifde simple and easy...than using the CreateThread and unrelaible TerminateThread functions.
Try it out.
VinExpert
U change the code to do something like this and see...
1. Have the event handles created in ur application.
2. In the function instead of CreateThread use AfxBeginThread function to launch the thread to simulate the wait. Before that, set the events to reset state by ResetEvent(...) call
3. Then use WaitForMultipleObject(...)
4. In the simulator thread, if successful, set one event and if fail set another and use AfxEndThread() to terminate the thread.
5. In the function, now the WaitForMultipleObject will return with the successful or fail or timeout events. Just check that.
This will make lifde simple and easy...than using the CreateThread and unrelaible TerminateThread functions.
Try it out.
VinExpert
ASKER
MichaelS:
I don't know a other method, If you have one, please say it to me.
VinExpert:
It's not possible to do that because I don't want that a DCOM call which takes to long time is blocing my thread.
To simulate the DCOM timeout, I can only kill the thread.
With your method (events), If the DCOM takes to long times to terminate, the event timeout will only fired after the DCOM call has terminated because I don't can stop it.
Here's a shema off what I want :
.
.
.
START DCOM THREAD
CALL DCOM FUNCTION
IF DCOM SUCCEED, THEN FILL INFO STRUCTURE
WAIT FOR THREAD TO TERMINATE
IF THREAD TERMINATED, INFO STRUCTURE IS VALID
IF WAIT TIMEOUT, THEN KILL THREAD BECAUSE DCOM FUNCTION IS STILL RUNNING.
.
.
.
I don't know a other method, If you have one, please say it to me.
VinExpert:
It's not possible to do that because I don't want that a DCOM call which takes to long time is blocing my thread.
To simulate the DCOM timeout, I can only kill the thread.
With your method (events), If the DCOM takes to long times to terminate, the event timeout will only fired after the DCOM call has terminated because I don't can stop it.
Here's a shema off what I want :
.
.
.
START DCOM THREAD
CALL DCOM FUNCTION
IF DCOM SUCCEED, THEN FILL INFO STRUCTURE
WAIT FOR THREAD TO TERMINATE
IF THREAD TERMINATED, INFO STRUCTURE IS VALID
IF WAIT TIMEOUT, THEN KILL THREAD BECAUSE DCOM FUNCTION IS STILL RUNNING.
.
.
.
>IF WAIT TIMEOUT, THEN KILL THREAD BECAUSE DCOM FUNCTION IS STILL RUNNING.
This one I don't like. May be you have to increase timeout or probably make a pool of not completed requests or some othe stuff. Or may be you can show the place where you make a call of DCOM function?
This one I don't like. May be you have to increase timeout or probably make a pool of not completed requests or some othe stuff. Or may be you can show the place where you make a call of DCOM function?
ASKER
My application, is a watchdog application which communicates via DCOM with 2 DCOM servers. Each server check is made in a separated thread. Each separated thread calls a lot of DCOM functions. I can not wait for a DCOM call indefinitivelly (the DCOM timeout is greather than 1 minute, which is to big for a watchdog).
I want to test it with AfxBeginThread, but I don't know how to kill the returned CWinThread if a timeout occured.
I want to test it with AfxBeginThread, but I don't know how to kill the returned CWinThread if a timeout occured.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This Guy is having the same problem I think
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20084406
mike_marquet,
These questions are still open and our records show you logged in recently. Please resolve them appropriately as soon as possible. Continued disregard of your open questions will result in the force/acceptance of a comment as an answer; other actions affecting your account may also be taken. I will revisit these questions in approximately seven (7) days. Please note that the recommended minimum for an "Easy" question is 50 points.
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11532221
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11701138
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11841838
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11928102
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11931818
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11996498
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11998538
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12011459
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12039459
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20005590
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20006135
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20071202
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20084832
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20091412
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20093693
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20095151
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20099190
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20099158
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20100338
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107694
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20114039
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20071199
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20116007
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20128382
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20137131
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20155355
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20192497
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20193603
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20238400
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20239471
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20245231
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20253153
EXPERTS: Please leave your thoughts on this question here.
Thanks,
Netminder
Community Support Moderator
Experts Exchange
These questions are still open and our records show you logged in recently. Please resolve them appropriately as soon as possible. Continued disregard of your open questions will result in the force/acceptance of a comment as an answer; other actions affecting your account may also be taken. I will revisit these questions in approximately seven (7) days. Please note that the recommended minimum for an "Easy" question is 50 points.
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11532221
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11701138
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11841838
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11928102
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11931818
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11996498
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11998538
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12011459
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12039459
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20005590
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20006135
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20071202
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20084832
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20091412
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20093693
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20095151
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20099190
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20099158
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20100338
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107694
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20114039
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20071199
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20116007
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20128382
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20137131
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20155355
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20192497
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20193603
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20238400
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20239471
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20245231
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20253153
EXPERTS: Please leave your thoughts on this question here.
Thanks,
Netminder
Community Support Moderator
Experts Exchange
Deletion rejected; comment force/accepted.
Netminder
CS Moderator, EE
Netminder
CS Moderator, EE