Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EAccessViolation in a class overloaded constructor. 1

Status
Not open for further replies.

ashkar

Programmer
Mar 28, 2001
6
0
0
CL
Hi,

Although I've been doing some stuff with Delphi, I thin kthis is the first time I'm defining a subclass directly from TObject. Well, I have the following:

type

TTrack = class(TObject)
public
Name: AnsiString;
Loops: TObjectList;
FXs: TObjectList;
constructor Create(inName: AnsiString); overload;
destructor Destroy; override;
private
end;

....

implementation

constructor TTrack.Create(inName: AnsiString);
begin
inherited Create;
Name := inName;
Loops := TObjectList.Create;
FXs := TObjectList.Create;
end;


When I create an object of this class, when I try to assign a property in the constructor, like "Name := inName;" above, I get an EAccessViolation exception. I've gone through the help, looked at some tutorial and other source code and I can't see what the problem is. It is supposed that TObject.create allocates memory, and I am calling it at the begining. What is it that I am doing wrong?

Thanks,

Ashkar
 
How are you creating an instance of this class? (It sounds like you really aren't creating one.)

In the declaration of your TForm1 I would expect to see something like:
Code:
  MyTrack:TTrack;
Then somewhere in your code I would expect to see:
Code:
  MyTrack := TTrack.Create( 'MyTest' );
I set up a test file with those lines (along with your code) and then included the following:
Code:
procedure TForm1.FormCreate(Sender: TObject);
begin
  MyTrack := TTrack.Create( 'MyTest' );
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := MyTrack.Name;
end;
Clicking Button1 resulted in Edit1 displaying 'MyTest' as expected.
 
I can't believe it. I am ashamed of myself. The instance is created in another unit, but, perhaps due to too much coffee o too little sleep I wrote:

newTrack.Create('trackname');

instead of

newTrack := TTrack.Create('trackname');

and since the exception is not generated in that line, but in the "Create" code instead, I didn't see it. Sorry for wasting your time with such nonsense, and thanks a lot for making me catch this error :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top