Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why EAccessViolation in Delphi ? (simple program attached)

Posted on 2003-03-11
10
Medium Priority
?
375 Views
Last Modified: 2010-04-04
The unit below compiles, but when the program is run, I get an EAccessViolation error message. Why is this?

I created a similar program in Java, which runs without an error. (I'm new to Delphi - have worked in Pascal for a number of years.)

I have tried different variations. The problems occurs as soon as any value is assigned to the "total" property of class "counter".

Please help - thanks.

Contents of unit is shown below (only one unit in the program):

unit Unit1; {program has one button, one label; label displays how many times the button has been clicked}

interface

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

type

    TItemcount = class {this is a class which holds
     the total no of times the button is clicked}
      total: integer;
      procedure inctotal();

    end;


  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);



  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  counter: TItemcount;


implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
        counter.total := 0;  {set counter to 0 at start of program}
end;


procedure TItemcount.inctotal();
  begin
     counter.total := counter.total + 1;
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   counter.inctotal();  {each time button is clicked increment the counter}
   label1.Caption := InttoStr(counter.total);
end;


end.
 
0
Comment
Question by:shearar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 1

Expert Comment

by:SimesA
ID: 8110324
You don't instantiate anything for Counter. Counter is a pointer to a class object, so you have to create the object that counter points to.

try adding the following at the very end of the unit.

initialization
  Counter := TItemcount.Create;

finialization
  FreeAndNil(Counter);


Alternatively, simply make Counter an integer (or longint, cardinal etc) since these types don't need creating.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8110598
I'll sugest more "Delphi like" code:

TItemcount = class(TObject)
  private
    FTotal: Integer;
  public
    constructor Create;virtual;
    procedure IncTotal(Delta: Integer = 1);
    property Total: Integer read FTotal;
end;

TForm1 = class(TForm)
   Button1: TButton;
   Label1: TLabel;
   procedure Button1Click(Sender: TObject);
   procedure FormCreate(Sender: TObject);
 private
   public
     FCounter: TItemcount;
   { Public declarations }
 end;

var
 Form1: TForm1;
 
implementation

{$R *.dfm}

constructor TItemCount.Create;
begin
  FTotal:=0;
end;

procedure TItemCount.IncTotal;
begin
  Inc(FTotal,Delta);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  FCounter:TItemCount.Create;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  FCounter.IncTotal;  {each time button is clicked increment the counter}
  label1.Caption := IntToStr(FCounter.Total);
end;


end.


ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8110614
should be:

.
.
.
procedure TForm1.FormCreate(Sender: TObject);
begin
 FCounter:=TItemCount.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FCounter.Free;
end;
.
.
.

ziolko.
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.

 
LVL 1

Expert Comment

by:SimesA
ID: 8110755

Since Counter is declared at the Unit level, not the form, it's better to make the unit manage the creation and destruction, not the form.

Your "more Delphi like" assumes that there is only ever one instance of Form1 at a time. This may be the case, but can you guarantee it?

0
 
LVL 21

Expert Comment

by:ziolko
ID: 8110797
SimesA > FCounter is private field in TForm1 so each instance of form will create its own FCounter.

ziolko.
0
 

Author Comment

by:shearar
ID: 8110943
Wow, this is incredible! First time I'm posting a question, and it's great to be helped like this. :-)

It's the first year that I'm teaching Delphi. My class are Grade 10's (14 & 15 yrs, no previous programming).

Would like to teach them the basic principles of OOP. The solution posted by SimesA ("more Delphi-like code") seems to be the correct one in terms of teaching OOP.

However, is it at all possible to make this a simpler example? The new concept here is the creation of one's own class, and accessing its properties.

One has to try and keep the number of new issues in the code as few as possible.

Will run the code by SimesA tonight (busy setting an exam paper), and send an "Accept as answer" after that.

Thanks again!

0
 
LVL 1

Expert Comment

by:SimesA
ID: 8111095
Sorry ziolko, I hadn't noticed that you'd moved the counter - I was referring to the original.
0
 
LVL 21

Accepted Solution

by:
ziolko earned 1000 total points
ID: 8111534
SimesA > I thought so :-)
shearar > "more Delphi-like code" > was my comment :-)
Hmmm OOP basics maybe some inheritance?:-)

TCountItem = class(TObject)
 private
   FTotal: Integer;
 public
   constructor Create;virtual;
   procedure IncTotal(Delta: Integer = 1);
   property Total: Integer read FTotal;
end;

TCountItemS = class(TCountItem)
  private
    FFmtString: string;
  protected
    function GetAsString:string;
  public
    constructor Create;override;
    property AsString: string read  GetAsString;
    property StringFormat: string read FFmtString write FFmtString;
end;

function TCountItemS.GetAsString:String;
begin
  Result:=Format(FFmtString,[Total]);
end;

constructor TCountItemS.Create;
begin
  inherited Create;
  FFmtString:='Total number: %d';
end;

ziolko.
0
 

Author Comment

by:shearar
ID: 8112766
I really like the inheritance bit - am still busy with the exam paper, but am going to try all of this tomorrow.

Thanks, guys!!  :-)
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8112915
glad to be helpfull
ziolko.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

704 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