Re: CInternetSession



ARGH! I'd missed that infinite loop! This is clearly a bad design.

In this case, the presence of the Sleep(10000) indicates the serious design flaw. The
correct model would be to simply use an OnTimer event that would trigger the next
iteration, and use a timer interval of 10000 ms.

It never ceases to amaze me how programmers try to cram everything into the single-thread
sequential-execution model they learned in their freshman programming course. I spend a
lot of time in my courses teaching students to think asynchrously and event-driven.

My standard advice: "If your multithreaded system won't work without liberally tossing
Sleep() calls around like pixie dust, your design is fundamentally broken and will need to
be corrected. If you use Sleep() in a loop, your design is probably wrong and needs to be
corrected. If you use Sleep() in the main GUI thread, your design is flawed beyond
recovery and needs to be rewritten. Assume that, with incredibly rare exceptions, Sleep()
is always a mistake."

I do this after one of the labs fails and can be fixed by tossing a Sleep() call at it. I
let them do a Sleep(10000). Lo and behold, the program works! Then I explain why this
shows that their fundamental design was wrong and then I explain why this solution is
wrong, and challenge them to come up with a guaranteed *correct* solution. After they
finally figure out WFMO, I then explain why IT has problems, and why blocking the main
thread is going to be a complete disaster. From this we work up to asynchronous
reference-counted notificaitons. Then we discuss robustness issues with that approach.
joe

That's a close enough approximation to reality to suffice for nearly all programming
tasks. And it gives them a simple predicate that even low-experience programmers can
test: if there's a Sleep, it's wrong. The number of exceptions to this rule is
vanishingly small.
joe

On Sun, 27 May 2007 16:15:04 -0500, "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote:

On Sun, 27 May 2007 17:06:14 +0200, SOCAR <socar@xxxxx> wrote:

I have tried below code before (which comply with your questions)
try{
while(true)
{
CInternetSession csiSession;
CHttpFile* MyFile = (CHttpFile*)
csiSession.OpenURL(_T("http://www.google.com";));
if( MyFile != NULL )
{

MyFile->Close();
delete MyFile;
}
}
}
catch(CInternetException*
e){MessageBox(NULL,_T("goterror"),_T("goterror"),MB_OK);
}
Sleep(10000);
}

but still I receive "First Chance Exception" warning. Also app have memory
leak about 4KB per loop (shown in TaskManager). The catch block is not
utilized in any way - app dont raise any exceptions during execution. Also
it returns a normal data from the http request via CStdioFile or CHttpFile.
Only thing is that it got this memory leaks, which "dissapears" when I
comment out "OpenURL" Line.

The "First Chance Exception" message usually indicates nothing harmful. See
this KB article for more on the subject:

First and second chance exception handling
http://support.microsoft.com/kb/105675

Your catch clause isn't being entered because something else is handling
the exception, probably a C++ exception handler established by OpenURL or
one of the functions it calls. You can configure the debugger to stop at
first chance exceptions or simply trace into the function to determine the
source of the exception, but what you're observing is exactly what you'd
expect to see if OpenURL (or a function it calls) throws an exception and
catches it internally. This is all roughly equivalent to a function
printing a message visible in the debugger whenever it returns an error
code, whether or not the error is eventually handled.

As for your memory leak, your loop is not returning to the application
message loop, so MFC is not performing its idle-time processing, during
which it frees temporary objects. I'd guess that's the cause, and you can
test this hypothesis by adding the following to the end of your loop:

// I'm assuming you're doing this in your app's primary thread.
LONG idleCount = 0;
while (AfxGetApp()->OnIdle(idleCount++))
continue;

If the leak doesn't go away, try using MFC's CMemoryState class to
determine the objects you're leaking.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: determine if file is being used by another process
    ... > never be predicated on exception handling, especially when there is a way ... between - see later] and only terminate the loop when the ... API like CreateFile is a closed loop without some sleep in between the ... such a way that it can dramatically increase the file creation time when run ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I gently terminate a thread???
    ... > Say for instance I have a thread that I fire off that's running some loop ... which would then have the thread exit. ... You could throw an exception that is only caught in the main method of the ... sleep on the thread will then throw a ThreadInterruptedException. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I gently terminate a thread???
    ... > Say for instance I have a thread that I fire off that's running some loop ... which would then have the thread exit. ... You could throw an exception that is only caught in the main method of the ... sleep on the thread will then throw a ThreadInterruptedException. ...
    (microsoft.public.dotnet.general)
  • Re: The origins of CL conditions system
    ... Machine experience explaining the Lisp Machine error handling. ... languages with continuable exceptions (including Mary Fontana from TI ... Why can't I resume after catching an exception? ... exception handling chapter of The Design and Evolution of C++. ...
    (comp.lang.lisp)
  • Re: Determining Plant Bandwidth
    ... I'm designing a pole placement controller, with a 1kHz control loop ... I question your assertion that your plant behavior is really a straight 20dB/decade line. ... Pole placement design only works to the extent that you know your plant characteristics. ... Unfortunately it is one of the worst methods that I know of for designing robust control systems. ...
    (sci.engr.control)