Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 864
  • Last Modified:

Hosting web browser control in a separate thread.

The following code creates a web browser
control in a thread separate from the main
UI thread.  When the window containing the
web browser control is closed, the following
error occurs:

"Unhandled exception in htmltester.exe: 0xC0000005: Access Violation."

This crash only seems to occur under Windows XP and
does not seem to occur when the control is hosted
in the main UI thread.

What am I doing wrong and how can I fix the
problem?

----------------------------------------------------
#include "stdafx.h"
#include "resource.h"
#define _ATL_APARTMENT_THREADED
#include <atlbase.h>
CComModule _Module;
#include <atlwin.h>

#define GENERIC_WINDOW_NAME                    "Generic Window"
#define HTML_WINDOW_NAME                    "HTML Window"
#define GENERIC_WINDOW_CLASS               "GenericWindowClass"
 
LRESULT CALLBACK WndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{    
     switch( uMsg )
     {
     case WM_CLOSE:
          {
               ::DestroyWindow( hwnd );
          }
          break;
     case WM_DESTROY:
          {
               PostQuitMessage(0);
          }
          break;
     default:
          {
               return ( DefWindowProc( hwnd, uMsg, wParam, lParam ) );
          }
     }
     return 0;
}

ATOM RegisterWindowClass( HINSTANCE hInstance )
{
     WNDCLASS wc;
     wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = ::LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = GENERIC_WINDOW_CLASS;      

     return RegisterClass(&wc);
}

DWORD WINAPI HTMLThreadProc( LPVOID lpParameter )
{
     // Get teh instance
     HINSTANCE hInstance  = (HINSTANCE) lpParameter;
     
     // Create the HTML Window
     HWND hWnd= CreateWindow( GENERIC_WINDOW_CLASS, HTML_WINDOW_NAME, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
     
     // Add ATL support
     //_Module.Init( 0, hInstance );
     AtlAxWinInit();
     
     // Add the HTML control
     RECT rtWindow;
     ::GetClientRect( hWnd, &rtWindow );
     CAxWindow ATLWnd;
     ATLWnd.Create( hWnd, rtWindow, _T("http://www.msn.com"), WS_VISIBLE | WS_CHILD );
     
     // Show the window
     ShowWindow( hWnd, SW_SHOW );
     UpdateWindow( hWnd );
     
     // Enter the mian message loop
     MSG msg;
     while( GetMessage( &msg, NULL, 0, 0 ) )
     {
          TranslateMessage( &msg );
          DispatchMessage( &msg );
     }

     //_Module.Term( );
     return msg.wParam;
};

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
     // Register the window class
     RegisterWindowClass( hInstance );

     // Create the HTML Window
     DWORD dwThreadID;
     HANDLE hThread = ::CreateThread( NULL, 0, HTMLThreadProc, hInstance, 0, &dwThreadID );
         
     // Create the main thread window
     HWND hWnd= CreateWindow( GENERIC_WINDOW_CLASS, GENERIC_WINDOW_NAME, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
     
     // Show the window
     ShowWindow( hWnd, nCmdShow );
     UpdateWindow( hWnd );
     
     // Enter the mian message loop
     MSG msg;
     while(GetMessage( &msg, NULL, 0, 0 ) )
     {
          TranslateMessage( &msg );
          DispatchMessage( &msg );
     }
     
     // Wait for the HTML thread to terminate
     ::WaitForSingleObject( hThread, INFINITE );
     ::CloseHandle( hThread );

     return 1;
}
----------------------------------------------------
0
azmajere
Asked:
azmajere
  • 5
  • 4
  • 2
  • +3
1 Solution
 
ChizlCommented:
"Unhandled exception in htmltester.exe: 0xC0000005: Access Violation."

This means you created an instance of something without closing or doing a .release() on it.  I would look at your CreateWindow, ATLWnd.Create, CreateThread, and anything else your opening that you possibly are not releasing.
0
 
azmajereAuthor Commented:
All of the code is given above.  I have looked over the code a few times and can not seem to pinpoint where such an error might be occurring.
0
 
ChizlCommented:
What about the:
//_Module.Init( 0, hInstance );
that you have..

Try:
_Module.Init( NULL, hInstance );
with the:
_Module.Term();


You may also want to try to put in a:
LRESULT OnDestroy( UINT, WPARAM, LPARAM, BOOL& ){
PostQuitMessage( 0 );
return 0;
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
azmajereAuthor Commented:
The _Module.Init and _Module.Term do seem to have any effect.  I guess technically that they should not be
commented out, I was just playing around trying to
get it work.

The "WM_DESTROY" message does appear to be handled
proper and is posting a "PostQuitMessage(0)".

0
 
BeyondWuCommented:
No problem on my PC(W2k and XP) if I un-comment "_Module.Init" and "_Module.Term".
I think "_Module.Init" and "_Module.Term" shouldn't be commented.
Try to rebuild it and test it on another computer.
0
 
azmajereAuthor Commented:
Uncommented the "_Module.Init" and ""_Module.Term"
statements.

Ok, did the test on 5 systems (2 Windows 2000 and 3
Windows XP ).  Both the Windows 2000 systems worked fine.
Two of the Windows XP systems crashed upon closing the
the HTML window.  One Windows XP systems seemed to have
no problems at all.

Obviously there is something machine(s) related. Did
you have dev studio installed on your test machines?

But why would it only occur when the control is hosted
in a secondary thread?

As, far as you can tell the there are no bugs in the code, correct?  
0
 
BeyondWuCommented:
It seems there no bug in the code. The exception maybe caused by ATL's code. Which version of VC/ATL are you using?
0
 
BeyondWuCommented:
Have you debugged it on XP? Where it stopped when exception caused? Is it crashed at _Module.Term( )?
0
 
azmajereAuthor Commented:
I am using Visual C++ version 6.0.  I have the latest
service packs and Platform SDK installed.

When it crashed under Windows XP the call stack is as
follows:

4A008AC8( )
NTDLL! 77fabb68( )

....yeah didn't help me much either :-(
0
 
BeyondWuCommented:
Try to install the debug symbol then debug it again.
Which is the latest instruction before crash?Try to put a breakpoint on return msg.wParam; Can you reach the breakpoint before crash?

0
 
DanRollinsCommented:
Does it make any difference if you create the Browser thread-and-window after the app main window is up and running?  I think I've read about certain peculiarities related to window creation order and threading.  Recall that XP has do do some underhanded shennanigans vis-a-vis the screwball window frame colors and cartoon buttons and such.

-- Dan
0
 
azmajereAuthor Commented:
1. The html window exits the thread properly.  It seems
to crashing after the thread exits...perhaps in an ie
thread in the background.

2. It makes no difference if the html thread is created
after the main window is visible.
0
 
DanRollinsCommented:
I don't have an XP system for testing, at it seems to have no problems on Win2000.

I commented out
       AtlAxWinInit();
and it ran OK.  The help said 'you don't always need to call this explicitly' and it seem at least possible that the problem is related to this.

Alternatively, you could try adding a call to
    AtlAxWinTerm( );
somewhere at the end.

=--==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
One other thing to try:  Shuffle the headers around a bit.  Perhaps placing

     #define _ATL_APARTMENT_THREADED
above
     #include "stdafx.h"
(and cleaning and rebuilding) might make a difference.

-- Dan
0
 
griesshCommented:
Dear azmajere

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to

     "refund the points and PAQ at zero points"

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
MindphaserCommented:
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now