Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help creating Constructor (Access violation at address ***)

520 views
Skip to first unread message

Mike Hlavenka

unread,
Aug 3, 2003, 7:45:50 PM8/3/03
to
First of all, I'm new at the Delphi envirnment. What I'm trying to do
is to create a simple class that encapsulates 2 components and some
other simple variables. However, at some points, I am getting the
good old "Access Violation" error. This error is occuring whenever I
try to access class variables. I've tried commenting out some things
and accessing them different ways, but every time I try to access a
variable that is a component (RichEdit and ComboBox), it gives me that
exception. I've tried creating them by passing "nil" and a form as
parameters, both didn't work. Also, I've put some code in between the
constructor beginning and when I first touch the components (such as
assigning simple integers), so it is not a problem of the constructor
declaration it seems.

Here is my code:

unit ListSelectionEntry;

interface

uses ComCtrls, Classes;

type
TListSelectionEntry = class

private
RichEdit1 : TRichEdit;
ComboBox1 : TComboBoxEx;
protected
public
constructor Create(length, relativeTop, relativeLeft : Integer;
mainForm : TComponent);
published
end;

implementation

constructor TListSelectionEntry.Create(length, relativeTop,
relativeLeft : Integer; mainForm : TComponent);
begin
if RichEdit1 = nil then // Exception comes here
begin
end;
RichEdit1 := TRichEdit.Create(nil); // Exception comes here too
with RichEdit1 do begin
//Parent := currentForm;
Top := relativeTop;
Left := 304;
Height := 20;
Width := 473;
end;
ComboBox1 := TComboBoxEx.Create(nil); // Exception also comes here
with ComboBox1 do begin
//Parent := TwinControl(currentForm);
Top := relativeTop;
Left := 160;
Height := 20;
Width := 145;
end;
end;

end.

Error Message:

Project xxxx raised exception class EAccessViolation with message
'Access violation at address 0049E617 in module '<My project's .exe>'.
Read of address 00000004' Process stopped. Use Step or Run to
continue.

I apologize for the sloppy parts of my code. I performed a bunch of
tests, so a lot of it is rearranged.

From what I can determine, it doesn't know that the pointers exist,
so, When I try to work with them, it gives me the exception. However,
I don't know why that would be.

Thank you for all that help in advance.

KLinZ

unread,
Aug 3, 2003, 8:29:55 PM8/3/03
to
Mike Hlavenka wrote:

> Error Message:
>
> Project xxxx raised exception class EAccessViolation with message
> 'Access violation at address 0049E617 in module '<My project's .exe>'.
> Read of address 00000004' Process stopped. Use Step or Run to
> continue.

C'est strange! The code doesn't give me any exception (and why would it?).


--
www.zenobits.com

Maynard Philbrook

unread,
Aug 3, 2003, 9:32:30 PM8/3/03
to
you should be calling Inherited first in your constructor so that the base
class will create the instance
of the main class first..

Rob Kennedy

unread,
Aug 4, 2003, 12:40:56 AM8/4/03
to
Maynard Philbrook wrote:
> you should be calling Inherited first in your constructor so that the base
> class will create the instance
> of the main class first..

You must be thinking of C++. By the time any code in the constructor is
running, the entire class already exists with all fields initialized to
all-bits-zero. Also notice that his TListSelectionEntry class descends
directly from TObject: the inherited constructor is an empty method, so
it would make no difference to this problem.

--
Rob

Rob Kennedy

unread,
Aug 4, 2003, 12:45:37 AM8/4/03
to
Mike Hlavenka wrote:
> constructor TListSelectionEntry.Create(length, relativeTop,
> relativeLeft : Integer; mainForm : TComponent);
> begin
> if RichEdit1 = nil then // Exception comes here
> [...]

> Error Message:
>
> Project xxxx raised exception class EAccessViolation with message
> 'Access violation at address 0049E617 in module '<My project's .exe>'.
> Read of address 00000004' Process stopped. Use Step or Run to
> continue.

You're creating your TListSelectionEntry object like the following,
aren't you?

var
entry: TListSelectionEntry;
begin
entry.Create(length, relativeTop, relativeLeft, mainForm);
end;

That can lead to the problems you're seeing. Create it like this
instead, just like you create the controls in your class's constructor.

entry := TListSelectionEntry.Create(...);

--
Rob

Mike Hlavenka

unread,
Aug 4, 2003, 7:48:46 AM8/4/03
to
Thank you Rob. I was concentrating on the wrong code. I didn't
really look at the code calling the class. I had something like

ListSelectionEntry.Create(20, 20, 20, Self);

instead of

Testing := ListSelectionEntry.Create(20, 20, 20, Self);

Thanks for pointing out this simple, simple mistake :).

Rob Kennedy <rken...@example.com> wrote in message news:<virp81o...@corp.supernews.com>...

AlanGLLoyd

unread,
Aug 4, 2003, 10:43:23 AM8/4/03
to
In article <c4fe056b.0308...@posting.google.com>,
mthla...@wisc.edu (Mike Hlavenka) writes:

>constructor TListSelectionEntry.Create(length, relativeTop,
> relativeLeft : Integer; mainForm : TComponent);
>begin
> if RichEdit1 = nil then // Exception comes here

RichEdit1 will always be nil here as a result of object initiation by Delphi.

> begin
> end;
> RichEdit1 := TRichEdit.Create(nil); // Exception comes here too
> with RichEdit1 do begin
> //Parent := currentForm;
> Top := relativeTop;
> Left := 304;
> Height := 20;
> Width := 473;
>

SetBounds(304, RelativeTop, 473, 20); // saves a few lines of code

Alan Lloyd
alang...@aol.com

Psyqwix

unread,
Sep 1, 2003, 10:38:48 AM9/1/03
to
When I first started using delphi the same problem always wigged me out
too. The reason you were having problems is because when you use the
create method, the object is created and a pointer is returned. I think,
because you weren't assigning the create statement to a variable the
pointer wasn't stored for that object, so when you referred to the
variable it simply pointed at junk (unless initialized).

I tried looking at the help files, but the definiton for TObject doesn't
specify if the constructor returns anything, when i'm convinced it does :S.

I know the problems been solved now, but I thought you might of liked
some insight into why :)

Psyqwix ):0) "Gluttony is an emotional escape, a signal that something
is eating us" - Peter de Vries

Rob Kennedy

unread,
Sep 1, 2003, 2:24:17 PM9/1/03
to
Psyqwix wrote:
> I tried looking at the help files, but the definiton for TObject doesn't
> specify if the constructor returns anything, when i'm convinced it does :S.

From the topic "Constructors":

<<
To create an object, call the constructor method in a class type. For
example,

MyObject := TMyClass.Create;

... the constructor returns a reference to the newly allocated and
initialized object. The type of the returned value is the same as the
class type specified in the constructor call.
>>

And later on:

<<
When a constructor is called using an object reference (rather than a
class reference), it does not create an object or return a value.
Instead, the constructor operates on the specified object, executing
only the statements in the constructor’s implementation.
>>

--
Rob

Psyqwix

unread,
Sep 1, 2003, 6:51:28 PM9/1/03
to
Cheers :)

Rob Kennedy wrote:
> Psyqwix wrote:
>
>> I tried looking at the help files, but the definiton for TObject
>> doesn't specify if the constructor returns anything, when i'm
>> convinced it does :S.
>
>
> From the topic "Constructors":
>
> <<
> To create an object, call the constructor method in a class type. For
> example,
>
> MyObject := TMyClass.Create;
>

> .... the constructor returns a reference to the newly allocated and

0 new messages