Setting Height property in .create constructor raises Access Violation Error

I have my own TForm descendant called TAdamForm.
In create() i would like to set height property.
If the MainForm is of type TAdamForm it raises Access Violation Error at that point.
The problem is, that this is hapening only on computers without Delphi installed and only in one project that uses TAdamForm as MainForm.

This is the code of TAdamForm.Create:

constructor TAdamForm.Create(AOwner: TComponent);
var MyH, MyW: integer;
  inherited Create(AOwner);
  if ScreenFakt<>1 then
  if ((MyW+left)>screen.Width) then
  if ((MyH+Top)>screen.WorkAreaHeight) then
      if (Top=1) and ((MyH+Top)>screen.WorkAreaHeight) then
          Height:=screen.WorkAreaHeight;  // this line produces an error
// i know that the problem is in Height because i wrote this instead:
// xx:=screen.WorkAreaHeight;
// Height:=xx;
// and error was raised at Height:=xx;
          if (ScreenFakt<>1) and (ScreenFakt>0) then
      if (Left=1) and ((Width+Left)>screen.WorkAreaWidth) then
          if (ScreenFakt<>1) and (ScreenFakt>0) then

For now I have solved the problem with removing this part of code. For aditional info I can say, that situation is the same if WindowState is wsNormal or wsMaximized

Any help would be apreciated
Who is Participating?
imitchieConnect With a Mentor Commented:
adam, can you try commenting out the OnResize code or just add Exit; to the beginning of the procedure, and see if you still get the Exception?

If your OnResize code refers to uninitialised things (form has never become visible), it will fail spectacularly when referenced in OnCreate.
MerijnBSr. Software EngineerCommented:
install (the trial version of) EurekaLog ( to find out where the error occurs on the machine without delphi installed.
I will suppose that adaform descendes from x1 which descendes from x2 and so on until xn which descendes from TForm or TCustomForm. n can be >=0 (if n=0 it means that adaform descends directly from tform or tcustomform).

does adaform or any of the xi (i=0..n) have a height property? if so, does any of them have a setter method? if so, what is the code from that setter method?

the curious thing is: how do you know that the error happens exactly on that line? what tipped you of about that line?
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 The big issue is that what you are doing should cause no access violation.  It is likely due to code that responds to the change in size (somewhere on your form).  Since the error does not happen while running under the IDE I would try setting the caption to the name of each of the form response methods as the first line in each of those methods.  You may need to include sub control response methods if they, in turn, respond to a change in the size.  If you do this and cause the error you will have the name of the routine it was last in, and this may surprise you.  Check the code there for access to something that would not exist yet and I think you will have your culprit.

Let me know if you need more.
do you have an event onResize?
adamsoftsiAuthor Commented:
Thanks for answers.
I would like to explain to all of you how did i locate the error.
I have put som log writeing betwen each line of code. Then when error occured on computer I waited for 10s to click OK on Access Violation Error. So I got 10s time difference betwen each log entry and I was able to define line with error.
TAdamForm is descendent of TForm.
TAdamForm has no Height property.

So does this data help you?
helps me by rulling out what I have thought to cause the problem. However developmentguru has also a good point which kind of includes imitchies one too.
MerijnBSr. Software EngineerCommented:
so you did find which line it was which started the error, but it's still very likely that the error actually occurs somewhere else (like developmentguru, imitchie and ciuly mean).
Please install eurekalog, it will tell you exactly where the error originated (
or madexcept, if you don't like eurekalog :)
MerijnBSr. Software EngineerCommented:
whichever :)
adamsoftsiAuthor Commented:

Yes I do have onResize event!

Do you belive there is a problem?
I have to say that on some other project there is similar problem on few machines (5 out of 500) where access violation error ocures after restoring from screen saver. We ware unable to isolate the error and those customer yust live with it. Your idea makes me think that this is connected.

What do you think it could be?
well, eitehr throw in some code, or some output from eurekalog/m,madexcept containing stracktrace of the error
 I strongly agree with those suggesting you use EurekaLog or MadExcept (I have used EurekaLog and it will be part of my toolbox from now on I believe).

  If you are unable to do anything like that now, go back to old fasioned debugging methods.  write lines to a log file as you enter each method on your main form.  Make sure to close the log file when you close the form.  If you also trap the unhandled exceptions and log them then you will see which routine was entered just before the exception.

  I strongly believe that the error is related to code that responds to the resize or something similar.  If you do not have much in the way of such code (say 3 methods) then you could try putting a new first line in each (Exit;).  If the error goes away then you know it was one of those methods.  You can then reintroduce them one at a time and see when the error comes back.  From there locate the error.

As far as the other project goes, do the same tests.  If it is written by the same developer than the likelyhood exists that the same form of error is happening.

Let me know if you need more.
My 2 cents, why must this be included in the OnCreate event?

Try OnShow.

MerijnBSr. Software EngineerCommented:
see attached snippet.
It's a piece of code from an application which does more or less what you need.

It's starts an application (in this case called bartender) and makes it appear in Panel called PanelBartender, which you can place anywhere in your form.
procedure TMainForm.StartBartender;
var StartupInfo: TStartUpInfo;
    ProcessInformation: TProcessInformation;
    Style: integer;
 if not FileExists(fBartenderLocation) then Exit;
 ZeroMemory(@StartupInfo, sizeof(StartupInfo));
 StartupInfo.cb := sizeof(StartupInfo);
 CreateProcess(pchar(fBartenderLocation), PChar(COMMANDLINE), nil, nil, false, 0, nil, nil, StartupInfo, ProcessInformation);
 fBartenderProcessHandle := ProcessInformation.hProcess;
 fBartenderProcessID := ProcessInformation.dwProcessId;
 WaitForInputIdle(fBartenderProcessHandle, INFINITE);
 fBartenderWndHandle := GetWindowHandle(fBartenderProcessID);
 if fBartenderWndHandle = 0 then
  fBartenderWndHandle := FindBartenderByTitle();
 Style := GetWindowLong(fBartenderWndHandle, GWL_STYLE);
 Style := Style and (not (WS_CAPTION));
 SetWindowLong(fBartenderWndHandle, GWL_STYLE, Style);
 Windows.SetParent(fBartenderWndHandle, PanelBartender.Handle);
 SetWindowPos(fBartenderWndHandle, 0, 0, 0, PanelBartender.ClientWidth, PanelBartender.ClientHeight, SWP_NOZORDER or SWP_SHOWWINDOW);
 ShowWindow(fBartenderWndHandle, SW_MAXIMIZE);

Open in new window

adamsoftsiAuthor Commented:
Hi, sorry for late response.
I have to find the problem by my self, but it helped to look in onResize event whati did not come to me without your help.
Thanks again
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.