Re: CWinThread and Memory Leaks.

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 01/14/05


Date: Thu, 13 Jan 2005 20:56:23 -0600

jim_OLP wrote:

>Thanks for the reply, I appreciate your expertise and hope you can get
>me out of this deadlock.
>
>No inter-thread SendMessage whatsoever.

None that you've identified so far, anyway. :)

>This is all stuff I've done
>successfully in the past, at 6.0, and I can't seem to make it work
>now.
>
>My main thread creates a CWinThread-derrived class with AfxBeginThread.
>That thread creates a modeless dialog in InitInistance. Things are fine
>until the main thread enters a loop iinside a message handler (the
>reason for the progress thread. The loop has a generous Sleep() in it
>so it's not hogging the CPU.

Inside the Sleep, it's also not processing messages or doing much of
anything else. This is anathema to UI threads, which as much as possible,
should sit in a GetMessage loop at all times.

>After that, the modeless dialog in the
>'progress' thread is dead. Spy++ shows it never gets any messages. If I
>break into the debugger I see my progress thread blocked at a
>DefWndProc call to m_pfnSuper. And it's total fog to me after that
>point, as I don't know what m_pfnSuper represents. I suspect MFC is
>trying to pass unhandled messages to the main window, thinking it's the
>parent of my dialog, but that makes no real sense. When I create the
>modeless dialog I don't give it a parent.

I'm sure you could get to the bottom of this if you kept spelunking around,
but I'd move the time-consuming operation out of the UI thread and into a
worker thread, and I'd have the UI thread host the progress dialog for the
worker. The worker would then communicate with this window (or another
designated target window) mainly through a (smallish) user-defined
PostMessage interface. This approach is probably the closest you can get to
something that "just works".

This is a wild guess, and hopefully someone will correct me or expand on
this as appropriate, but ISTR that if you don't specify the main window for
a secondary UI thread, MFC sets it to the primary thread's main window. I
don't know if this could pose a problem, but if I were ever to create
multiple UI threads, I'd look into it.

-- 
Doug Harrison
Microsoft MVP - Visual C++


Relevant Pages

  • Re: Loop to load webpage
    ... a reference an already open IE window. ... Dim objShell As Object, objShellWindows As Object, o As Object ... > ' Loop until the page is fully loaded ...
    (microsoft.public.excel.programming)
  • Re: Pass Worksheet Button Caption or Identifier to Sub
    ... Rightclick on the righthand window and choose "Show Hidden Members" ... You could loop through all the shapes on the worksheet. ... dim BTN as Button ... Sub ClickProc() ...
    (microsoft.public.excel.programming)
  • Re: Deaf CAsyncSocket on Windows Service.
    ... unlucky timing coincidence between Send, OnReceive, Receiveand data ... encountered) is that the OnReceive notification is a message in the Windows ... If OnReceive somehow lets a message loop run (such as by ... Not sure how this notification could be dropped; it is a window message, ...
    (microsoft.public.vc.mfc)
  • Re: Why does this TextOut() sometimes hang up?
    ... Call periodically in your loop. ... when the window ... // virtual window device context ... ScreenWidth = GetSystemMetrics; ...
    (microsoft.public.vc.mfc)
  • Re: Problem with Image display on separate window
    ... IE7 puts an address bar at the top of the picture below the title bar. ... I am curious as to why the large image loads quick after the first time it ... is when you are in a loop (while loop or for loop of x iterations to ... is there some type of hide window function? ...
    (microsoft.public.dotnet.framework.aspnet)