Solved

EAccessViolation when accessing an object from an event that has been called from it's constructor

Posted on 2002-03-18
2
199 Views
Last Modified: 2010-04-05
Hi,

I have a class and sometimes an event can be fired from within the constructor itself, so I allow the event property to be set with a parameter in the constructor.  The problem is that in the event, accessing the StringList gives an EAccessViolation, even though the StringList itself has been created before the event is called.  I can access the StringList within Create though.

The code below demonstrates the problem.  Do you know of any way to get around this problem?

Thanks,

Richard



unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }

    Test: TTest;

    procedure TestEvent(i: Integer);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Test := TTest.Create(TestEvent);
  Test.Free;
end;

procedure TForm1.TestEvent(i: Integer);
begin
  // This line causes the access violation

  ShowMessage(IntToStr(Test.StringList.Count));
end;

end.

unit Unit2;

interface

uses
  classes;

type TOnTestEvent = procedure(i: Integer) of object;

type
  TTest = class
  private
    FOnTestEvent: TOnTestEvent;
  public
    StringList: TStringList;

    property OnTestEvent: TOnTestEvent read FOnTestEvent write FOnTestEvent;

    constructor Create(AOnTestEvent: TOnTestEvent);
    destructor Destroy; override;
  end;

implementation

uses
  Dialogs, SysUtils;

constructor TTest.Create(AOnTestEvent: TOnTestEvent);
begin
  OnTestEvent := AOnTestEvent;

  StringList := TStringList.Create;

  // The StringList can be accessed here okay with no problems

  ShowMessage(IntToStr(StringList.Count));

  if Assigned(FOnTestEvent) then FOnTestEvent(1);
end;

destructor TTest.Destroy;
begin
  StringList.Free;
end;

end.
0
Comment
Question by:Richard2000
[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
2 Comments
 

Expert Comment

by:Kelmi
ID: 6878488
Reason is that the 'Test' member is still nil when you call it from the TestEvent procedure.

 ShowMessage(IntToStr(Test.StringList.Count)); // Test is still nil because it hasn't been created yet
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 50 total points
ID: 6879171
What Kelmi is sying is in fact the problem. The variable Test is only being assigned the constructed object when the constructor has successfully terminated. Therefore, Test is not assigned until after the constructor. To work around this, do it like this:

Unit1:

procedure TForm1.TestEvent(Sender: TObject; i: Integer);
begin
    ShowMessage(IntToStr((Sender as TTest).StringList.Count));
end;

Unit2:

type
    TOnTestEvent = procedure(Sender: TObject; i: Integer) of object;

constructor TTest.Create(AOnTestEvent: TOnTestEvent);
begin
    OnTestEvent := AOnTestEvent;
    StringList := TStringList.Create;
    ShowMessage(IntToStr(StringList.Count));
    if Assigned(FOnTestEvent) then
        FOnTestEvent(self,1);
end;

It's a good idea to include the Sender anyways since this allows you to have mutliple objects launch the same event without loosing knowledge of which object did fire the event.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reconfigure Delphi Install? 2 65
TServerSocket - file via sendbuf or Text via sendtext ? 1 76
SUM 2 INTEGER ARRAYS INTO 1 10 111
Firemonkey Queue list 1 28
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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