0

I create hidden IWebBrowser2 object and it work's fine but after few seconds I want to change visibility to true and my application crashes.

pBrowser2->put_Visible(VARIANT_TRUE);

What I'm doing wrong ?

Roman R.
  • 68,205
  • 6
  • 94
  • 158
DarkGL
  • 63
  • 5

1 Answers1

3

You're using pBrowser2 after releasing it.

You have:

pBrowser2->Release();  // pBrowser NOW INVALID!!!
srand( time( NULL ) );
//Sleep( ( std::rand() % 5000 ) + 5000 );
if(std::rand() % 100 <= chance ){
    pBrowser2->put_Visible(VARIANT_TRUE);  // instant crash here!
}

Move the call to Release() after you're done using it, or use a COM smart pointer so you don't have to manage it yourself. Simplest fix:

srand( time( NULL ) );
//Sleep( ( std::rand() % 5000 ) + 5000 );
if(std::rand() % 100 <= chance ){
    pBrowser2->put_Visible(VARIANT_TRUE);  // works
}
pBrowser2->Release();  // pBrowser NOW INVALID!!!
Eric Brown
  • 13,774
  • 7
  • 30
  • 71