Discussion:
AfxBeginThread failure - how to find cause?
(too old to reply)
UI Guy
2007-03-13 07:30:30 UTC
Permalink
In an MFC pgm under VS2005sp1, I am creating a worker thread using
AfxBeginThread. Under certain circumstances which I can't quite pin
down, AfxBeginThread returns NULL, indicating it has failed. Is there
any way I can find out the cause of the failure, such as an error
code? I have tried tracing through the code with the debugger, but
what is apparently detecting the error condition is in MS code for
which I don't have the source.

My specific code is

CWinThread* pThread=AfxBeginThread(MyControlFunction,&ti,0/*priority*/,
230000000/*stack size*/);

and a test for pThread==NULL immediately following. It is a rather
large stack size.

The thread terminates by returning from MyControlFunction.

Usually creation of the worker thread succeeds the first time, and
fails the second. There seems to be some interaction between failure
and use of the file open common dialog in some preceding code.
Scott McPhillips [MVP]
2007-03-13 14:19:55 UTC
Permalink
Post by UI Guy
My specific code is
CWinThread* pThread=AfxBeginThread(MyControlFunction,&ti,0/*priority*/,
230000000/*stack size*/);
and a test for pThread==NULL immediately following. It is a rather
large stack size.
Have you tried it without the large stack size?
--
Scott McPhillips [VC++ MVP]
UI Guy
2007-03-13 18:21:26 UTC
Permalink
Post by Scott McPhillips [MVP]
Have you tried it without the large stack size?
Yes, same result. Let me emphasize that with intermittent failures
like this one, it is important to identify the cause of the failure,
since you can't depend on a successful test run to prove the code
works.

That's why I want some error code or explanation of the AfxBeginThread
return.

Further, there is no reason why a large stack size should fail; if I
understand the docs, this is the reserve size, not the commit size.

Under VS2005, I don't seem to have any way of monitoring thread memory
allocations. And, I have found that VirtualQuery/Ex doesn't work as
documented, either.
Alexander Grigoriev
2007-03-13 16:35:41 UTC
Permalink
Just don't use such big stack.
Post by UI Guy
In an MFC pgm under VS2005sp1, I am creating a worker thread using
AfxBeginThread. Under certain circumstances which I can't quite pin
down, AfxBeginThread returns NULL, indicating it has failed. Is there
any way I can find out the cause of the failure, such as an error
code? I have tried tracing through the code with the debugger, but
what is apparently detecting the error condition is in MS code for
which I don't have the source.
My specific code is
CWinThread* pThread=AfxBeginThread(MyControlFunction,&ti,0/*priority*/,
230000000/*stack size*/);
and a test for pThread==NULL immediately following. It is a rather
large stack size.
The thread terminates by returning from MyControlFunction.
Usually creation of the worker thread succeeds the first time, and
fails the second. There seems to be some interaction between failure
and use of the file open common dialog in some preceding code.
UI Guy
2007-03-13 18:32:07 UTC
Permalink
Post by Alexander Grigoriev
Just don't use such big stack.
See previous reply; it doesn't matter.
AliR (VC++ MVP)
2007-03-13 20:07:02 UTC
Permalink
Did you try GetLastError after the call to AfxBeginThread.

Honestly I have never had AfxBeginThread fail on me.

AliR.
Post by UI Guy
In an MFC pgm under VS2005sp1, I am creating a worker thread using
AfxBeginThread. Under certain circumstances which I can't quite pin
down, AfxBeginThread returns NULL, indicating it has failed. Is there
any way I can find out the cause of the failure, such as an error
code? I have tried tracing through the code with the debugger, but
what is apparently detecting the error condition is in MS code for
which I don't have the source.
My specific code is
CWinThread* pThread=AfxBeginThread(MyControlFunction,&ti,0/*priority*/,
230000000/*stack size*/);
and a test for pThread==NULL immediately following. It is a rather
large stack size.
The thread terminates by returning from MyControlFunction.
Usually creation of the worker thread succeeds the first time, and
fails the second. There seems to be some interaction between failure
and use of the file open common dialog in some preceding code.
Tom Serface
2007-03-13 22:31:44 UTC
Permalink
I've never had it fail either. OP has said that the stack size is not the
issue. This is a tough one. I would try debugging down into the Afx code
and see what is happening down there.

Tom
Post by AliR (VC++ MVP)
Did you try GetLastError after the call to AfxBeginThread.
Honestly I have never had AfxBeginThread fail on me.
AliR.
UI Guy
2007-03-14 08:19:00 UTC
Permalink
Post by Tom Serface
I would try debugging down into the Afx code
and see what is happening down there.
How do you propose to see what's happening? There is a call to a
Windows function which does all the work. I don't have the sources, so
all I can see is disassembly.
Scott McPhillips [MVP]
2007-03-14 13:09:24 UTC
Permalink
Post by UI Guy
Post by Tom Serface
I would try debugging down into the Afx code
and see what is happening down there.
How do you propose to see what's happening? There is a call to a
Windows function which does all the work. I don't have the sources, so
all I can see is disassembly.
AfxBeginThread is an MFC function. The source code for MFC is provided.
If you can't single-step into it you don't have the source installed.
You may have to run the VC setup CD to select the option to install the
MFC source code. With it installed you can single step into
AfxBeginThread and look for reasons it might return NULL.
--
Scott McPhillips [VC++ MVP]
UI Guy
2007-03-15 08:29:42 UTC
Permalink
Post by Scott McPhillips [MVP]
AfxBeginThread is an MFC function. The source code for MFC is provided.
If you can't single-step into it you don't have the source installed.
Yes, I have this code installed and have already stepped into it. All
it does is call a few Windows routines; the error is detected in one
of those. So, without those sources, which are Top Secret, I can't get
any info.
Scott McPhillips [MVP]
2007-03-15 14:29:33 UTC
Permalink
Post by UI Guy
Post by Scott McPhillips [MVP]
AfxBeginThread is an MFC function. The source code for MFC is provided.
If you can't single-step into it you don't have the source installed.
Yes, I have this code installed and have already stepped into it. All
it does is call a few Windows routines; the error is detected in one
of those. So, without those sources, which are Top Secret, I can't get
any info.
An idea: Try temporarily putting in a call to CreateThread. It is the
Windows API that all thread creation calls ultimately lead to. If it
returns NULL you can then call GetLastError to get some diagnostic info.
--
Scott McPhillips [VC++ MVP]
Tom Serface
2007-03-14 14:31:58 UTC
Permalink
Yeah, what Scott said :o)

Seriously, you can step down into the code, but it will likely end up in a
call to a Windows SDK function that you won't have source for to browse, but
it may give you some insight. At least you might be able to check a return
code or something there.

Tom
Post by UI Guy
Post by Tom Serface
I would try debugging down into the Afx code
and see what is happening down there.
How do you propose to see what's happening? There is a call to a
Windows function which does all the work. I don't have the sources, so
all I can see is disassembly.
Alexander Grigoriev
2007-03-14 02:12:16 UTC
Permalink
GetLastError after AfxBeginThread isn't likely to give you CreateThread
error code.
Post by AliR (VC++ MVP)
Did you try GetLastError after the call to AfxBeginThread.
Honestly I have never had AfxBeginThread fail on me.
AliR.
Post by UI Guy
In an MFC pgm under VS2005sp1, I am creating a worker thread using
AfxBeginThread. Under certain circumstances which I can't quite pin
down, AfxBeginThread returns NULL, indicating it has failed. Is there
any way I can find out the cause of the failure, such as an error
code? I have tried tracing through the code with the debugger, but
what is apparently detecting the error condition is in MS code for
which I don't have the source.
My specific code is
CWinThread* pThread=AfxBeginThread(MyControlFunction,&ti,0/*priority*/,
230000000/*stack size*/);
and a test for pThread==NULL immediately following. It is a rather
large stack size.
The thread terminates by returning from MyControlFunction.
Usually creation of the worker thread succeeds the first time, and
fails the second. There seems to be some interaction between failure
and use of the file open common dialog in some preceding code.
UI Guy
2007-03-14 08:16:55 UTC
Permalink
Post by AliR (VC++ MVP)
Did you try GetLastError after the call to AfxBeginThread.
Yes, also @ERR,hr in debugger Watch window. Neither showed any error.
Loading...