Board index » delphi » Why am I getting an Address violation

Why am I getting an Address violation

Can anyone help with the following please. I have a main form from which I
occasionally need to create a subform with a Tchart object on it. As a
convention I always keep all of my procedures in seperate units so I have a
procedures unit for my main form and a second procedures unit for my
subform. As soon as I try and reference the subform Tchart in a subform
procedure I get an Address violation. I don't kow if I have explained that
well so I have added example listings of a little test program to try and
highlight the problem.
The main form has a single button it it to take you to the subform. The
subform has two buttons, one to return to the main form and the other to
change the axis maximum on it's chart.

program twoformsP;
uses
  Forms,
  twoforms in 'twoforms.pas' {Form1},
  subform in 'subform.pas' {mysubform},
  subformproc in 'subformproc.pas';

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

------------------------------

unit twoforms;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure CreateMDIchild(const Name: string);

  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
uses subform;

Procedure TForm1.CreateMDIchild(const Name: string);
var
   mysubchild : Tmysubform;
begin
     If name = 'mysubform' then
     begin
          mysubchild := Tmysubform.Create(Application);
          mysubchild.caption := Name;
          form1.hide;
          mysubchild.show;
     end
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     createMDIChild('mysubform');
end;

end.

------------------------------------------------------

unit subform;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, twoforms, ExtCtrls, TeeProcs, {*word*249}gine, Chart;

type
  Tmysubform = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Chart1: TChart;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  mysubform: Tmysubform;

implementation

{$R *.DFM}

uses subformproc;

procedure Tmysubform.Button1Click(Sender: TObject);
begin
     close;
     form1.show;
end;

procedure Tmysubform.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     if MessageDlg('Quit Sub Form?', mtConfirmation,
     [mbYes, mbNo], 0) = mrYes then
     begin
         {do any closing stuff here}
         Action := caFree;
     end
     else
         Action := caNone;
end;

procedure Tmysubform.Button2Click(Sender: TObject);
begin
     Changeaxis;
end;

end.

----------------------------------------------------

unit subformproc;

interface

uses subform;

procedure changeaxis;

implementation

procedure changeaxis;
begin
with mysubform do
begin
     Chart1.leftaxis.maximum := 100;
end;
end;

end.

 

Re:Why am I getting an Address violation


When you click Button1 on your Form1, the subform is created and assigned to
the LOCAL variable MySubChild.
Your GLOBAL variable MySubForm is never assigned to this form, so when you
address the chart, it refers to MySubForm, which is still NIL I guess. Hence
an address violation.
Tom

Quote
"Keith Howell" <keith_how...@btconnect.com> wrote in message

news:b88aa0$nqs$1@titan.btinternet.com...
Quote
> Can anyone help with the following please. I have a main form from which I
> occasionally need to create a subform with a Tchart object on it. As a
> convention I always keep all of my procedures in seperate units so I have
a
> procedures unit for my main form and a second procedures unit for my
> subform. As soon as I try and reference the subform Tchart in a subform
> procedure I get an Address violation. I don't kow if I have explained that
> well so I have added example listings of a little test program to try and
> highlight the problem.
> The main form has a single button it it to take you to the subform. The
> subform has two buttons, one to return to the main form and the other to
> change the axis maximum on it's chart.

> program twoformsP;
> uses
>   Forms,
>   twoforms in 'twoforms.pas' {Form1},
>   subform in 'subform.pas' {mysubform},
>   subformproc in 'subformproc.pas';

> {$R *.RES}

> begin
>   Application.Initialize;
>   Application.CreateForm(TForm1, Form1);
>   Application.Run;
> end.

> ------------------------------

> unit twoforms;

> interface

> uses
>   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
>   StdCtrls;

> type
>   TForm1 = class(TForm)
>     Label1: TLabel;
>     Label2: TLabel;
>     Button1: TButton;
>     Label3: TLabel;
>     procedure Button1Click(Sender: TObject);
>   private
>     { Private declarations }
>   public
>     { Public declarations }
>     procedure CreateMDIchild(const Name: string);

>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}
> uses subform;

> Procedure TForm1.CreateMDIchild(const Name: string);
> var
>    mysubchild : Tmysubform;
> begin
>      If name = 'mysubform' then
>      begin
>           mysubchild := Tmysubform.Create(Application);
>           mysubchild.caption := Name;
>           form1.hide;
>           mysubchild.show;
>      end
> end;

> procedure TForm1.Button1Click(Sender: TObject);
> begin
>      createMDIChild('mysubform');
> end;

> end.

> ------------------------------------------------------

> unit subform;

> interface

> uses
>   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
>   StdCtrls, twoforms, ExtCtrls, TeeProcs, {*word*249}gine, Chart;

> type
>   Tmysubform = class(TForm)
>     Label1: TLabel;
>     Label2: TLabel;
>     Button1: TButton;
>     Chart1: TChart;
>     Button2: TButton;
>     procedure Button1Click(Sender: TObject);
>     procedure FormClose(Sender: TObject; var Action: TCloseAction);
>     procedure Button2Click(Sender: TObject);
>   private
>     { Private declarations }
>   public
>     { Public declarations }
>   end;

> var
>   mysubform: Tmysubform;

> implementation

> {$R *.DFM}

> uses subformproc;

> procedure Tmysubform.Button1Click(Sender: TObject);
> begin
>      close;
>      form1.show;
> end;

> procedure Tmysubform.FormClose(Sender: TObject; var Action: TCloseAction);
> begin
>      if MessageDlg('Quit Sub Form?', mtConfirmation,
>      [mbYes, mbNo], 0) = mrYes then
>      begin
>          {do any closing stuff here}
>          Action := caFree;
>      end
>      else
>          Action := caNone;
> end;

> procedure Tmysubform.Button2Click(Sender: TObject);
> begin
>      Changeaxis;
> end;

> end.

> ----------------------------------------------------

> unit subformproc;

> interface

> uses subform;

> procedure changeaxis;

> implementation

> procedure changeaxis;
> begin
> with mysubform do
> begin
>      Chart1.leftaxis.maximum := 100;
> end;
> end;

> end.

Other Threads