Solved

Setting Height property in .create constructor raises Access Violation Error

Posted on 2007-11-14
16
235 Views
Last Modified: 2010-04-21
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
Comment
Question by:adamsoftsi
  • 4
  • 4
  • 3
  • +3
16 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 20278708
install (the trial version of) EurekaLog (http://www.eurekalog.com) to find out where the error occurs on the machine without delphi installed.
0
 
LVL 28

Expert Comment

by:ciuly
ID: 20278740
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
 
LVL 21

Expert Comment

by:developmentguru
ID: 20281670
 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
 
LVL 25

Expert Comment

by:imitchie
ID: 20282665
do you have an event onResize?
0
 

Author Comment

by:adamsoftsi
ID: 20282724
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
 
LVL 28

Expert Comment

by:ciuly
ID: 20283061
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
 
LVL 19

Expert Comment

by:MerijnB
ID: 20283083
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
 
LVL 28

Expert Comment

by:ciuly
ID: 20283752
or madexcept, if you don't like eurekalog :)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Expert Comment

by:MerijnB
ID: 20283764
whichever :)
0
 

Author Comment

by:adamsoftsi
ID: 20283815
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
 
LVL 28

Expert Comment

by:ciuly
ID: 20283955
well, eitehr throw in some code, or some output from eurekalog/m,madexcept containing stracktrace of the error
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 20284208
 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
 
LVL 18

Expert Comment

by:Johnjces
ID: 20351556
My 2 cents, why must this be included in the OnCreate event?

Try OnShow.

John
0
 
LVL 25

Accepted Solution

by:
imitchie earned 250 total points
ID: 20353679
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
 
LVL 19

Expert Comment

by:MerijnB
ID: 20356534
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
 

Author Closing Comment

by:adamsoftsi
ID: 31409161
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now