Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

12 Experts available now in Live!

Get 1:1 Help Now