Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible causes of "EOSError 1400 - Invalid window handle"

I have a problem. I have a VCL application using a thread. This thread does some calculations and calls a MainForm's method through Synchronize(). It all works just fine, I have a "Start" button, which creates and runs the thread and a "Stop" button which stops and frees it. No errors, no nothing.

But for some reason when I close the application and I've run the thread I get a EOSError 1400 - Invalid window handle. I've breakpointed the OnClose procedure and the only thing I do there is saving an ini file, no error in that, when I trace further (using F7), I get to the very end (Application.Run; end.), after "calling" the end. I get the error, so there is no specific line of code raising it.

I hope the question is somewhat clear and I hope it's solvable, because just ignoring the error seems a bit unclean.

Thanks inb4


ANSWER

This error occured to me when the Execute method of a thread was called, it looked like this:

procedure TRunThread.Execute;
var (...)
begin
  while not Terminated do begin
    (...)
    MainForm.Memo1.Lines.Add('Some text'); // Even though this call worked fine during
    //the application running, it caused errors on shutting the app down.
    // For acccessing GUI elements, it's necessary to use Synchronize()
    (...)
  end;
end;
like image 286
Martin Melka Avatar asked Jun 15 '11 06:06

Martin Melka


1 Answers

I had the same problem, error code 5 Access is denied. This turned out to related to a thread started to test an internet connection on an embedded panel (using BeginThread). If the user exits the form (which is testing the internet connection) immediately after displaying the form, the AV occurs.

On my development PC, the internet connection test was successful...and so fast I never saw the problem! After struggling for several hours, I finally tracked it down to this thread and reproduced it by disconnecting my network cable.

The solution was straightforward: When exiting the form (eg. in the FormDestroy event) ensure the thread is definitely not running before continuing.

like image 144
AlainD Avatar answered Sep 17 '22 12:09

AlainD