Solved

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

Posted on 2002-03-18
2
200 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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