BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Error with pointers

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
gajo
Guest





PostPosted: Sun Dec 21, 2003 12:47 pm    Post subject: Error with pointers Reply with quote



Hello, I'm new to pointers, and I have a problem that I think is easy to
solve. I get an EAccessViolation when trying to initialize my object with
the Init procedure. The carret points at "elem", or in other words, I think
I'm trying to access M.elem without M being initialized. But M IS
initialized, at least I think so!
So what's wrong?

type
Main = ^MainType;
MainType = class
private
connect: array[1..maxNum] of Main;
elem: integer;
public
procedure Init;
procedure KillObj;
end;

....

procedure MainType.Init;
var
i: integer;
begin
elem := 0;
for i := 1 to maxNum do
connect[i] := nil
end;

....

procedure Button1Click
var
M: Main;
begin
GetMem(M, SizeOf(M));
G.Init;
G.KillObj
end;


Back to top
Tom de Neef
Guest





PostPosted: Sun Dec 21, 2003 3:11 pm    Post subject: Re: Error with pointers Reply with quote



"gajo" <gajo (AT) eunet (DOT) yu> wrote

Quote:
Hello, I'm new to pointers, and I have a problem that I think is easy to
solve. I get an EAccessViolation when trying to initialize my object with
the Init procedure. The carret points at "elem", or in other words, I
think
I'm trying to access M.elem without M being initialized. But M IS
initialized, at least I think so!
So what's wrong?

type
Main = ^MainType
MainType = class
private
connect: array[1..maxNum] of Main;
elem: integer;
public
procedure Init;
procedure KillObj;
end;

...

procedure MainType.Init;
var
i: integer;
begin
elem := 0;
for i := 1 to maxNum do
connect[i] := nil
end;

...

procedure Button1Click
var
M: Main;
begin
GetMem(M, SizeOf(M));
G.Init;
G.KillObj
end;


If you want to try out pointers, let them point to records, not classes. A
class reference is itself a pointer. So SizeOf(M) will be the size of a
pointer. But in a record you can't have private parts or methods.
Anyway, what is the G you reference in Button1Click?
So, maybe you don't want pointers, but just classes. In that case check how
I modified your code below.
Note that I've named the type with a starting T as is customary in Delphi.
Tom

type

TMainType = class
private
connect: array[1..maxNum] of TMainType;
elem: integer;
public
constructor create;
procedure free;
end;

...

constructor TMainType.create;
var
i: integer;
begin
inherited;
elem := 0;
for i := 1 to maxNum do
connect[i] := nil
end;

procedure TMainType.free;
var i : integer;
begin
for i:=1 to maxNum do FreeAndNil(connect[i]);
inherited
end;

procedure Button1Click
var
M: TMainType;
begin
M:=TMainType.create;
M.Init;
M.free;
end;




Back to top
Duncan McNiven
Guest





PostPosted: Sun Dec 21, 2003 3:46 pm    Post subject: Re: Error with pointers Reply with quote



On Sun, 21 Dec 2003 16:11:40 +0100, "Tom de Neef" <tdeneef (AT) qolor (DOT) nl> wrote:

Quote:
constructor TMainType.create;
var
i: integer;
begin
inherited;
elem := 0;
for i := 1 to maxNum do
connect[i] := nil
end;

Why explicitly initialize the connect array? The Delphi help says "Because a constructor
sets the fields of a new object to zero or empty values before performing other actions,
class-type and pointer-type fields in a partially constructed object are always nil". Do
you have any reason to believe this is not so for arrays of class types? Or is this purely
for clarity / example?

Quote:
procedure TMainType.free;
var i : integer;
begin
for i:=1 to maxNum do FreeAndNil(connect[i]);
inherited
end;

Why implement Free instead of Destroy?
I think there is a bug here. Some programmers might rely on the documented behaviour of
Free,ie "Free checks for a nil reference before calling Destroy". Your code will give an
access violation if called on a nil object. Better to always put your clean-up code in an
over-ridden Destroy method rather than Free.

--
Duncan


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.