• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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;
begin
  inherited Create(AOwner);
  NaEkranu:=false;
  NastaviFraze;
  MyH:=Height;
  MyW:=Width;
  if ScreenFakt<>1 then
    begin
      MyH:=round(MyH*ScreenFakt);
      MyW:=round(MyW*ScreenFakt);
    end;
  if ((MyW+left)>screen.Width) then
    Left:=1;
  if ((MyH+Top)>screen.WorkAreaHeight) then
    Top:=1;
      if (Top=1) and ((MyH+Top)>screen.WorkAreaHeight) then
        begin
          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
            begin
              Height:=round(Height/ScreenFakt);
            end;
        end;
      if (Left=1) and ((Width+Left)>screen.WorkAreaWidth) then
        begin
          Width:=screen.WorkAreaWidth;
          if (ScreenFakt<>1) and (ScreenFakt>0) then
            Width:=round(Width/ScreenFakt);
        end;
end;

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
0
adamsoftsi
Asked:
adamsoftsi
  • 4
  • 4
  • 3
  • +3
1 Solution
 
MerijnBSr. Software EngineerCommented:
install (the trial version of) EurekaLog (http://www.eurekalog.com) to find out where the error occurs on the machine without delphi installed.
0
 
2266180Commented:
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?
0
 
developmentguruCommented:
 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.
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.

 
imitchieCommented:
do you have an event onResize?
0
 
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?
0
 
2266180Commented:
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.
0
 
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 (www.eurekalog.com).
0
 
2266180Commented:
or madexcept, if you don't like eurekalog :)
0
 
MerijnBSr. Software EngineerCommented:
whichever :)
0
 
adamsoftsiAuthor Commented:
imitchie

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?
0
 
2266180Commented:
well, eitehr throw in some code, or some output from eurekalog/m,madexcept containing stracktrace of the error
0
 
developmentguruCommented:
 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.
0
 
JohnjcesCommented:
My 2 cents, why must this be included in the OnCreate event?

Try OnShow.

John
0
 
imitchieCommented:
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.
0
 
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;
const COMMANDLINE = '/RUN /MAX /NOSPLASH';
var StartupInfo: TStartUpInfo;
    ProcessInformation: TProcessInformation;
    Style: integer;
begin
 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);
 
 SetForegroundWindow(Handle);
end;

Open in new window

0
 
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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