Firstly, you should not use pointers when using classes unless it is
absolutely necessary.  This is because an object reference is implicitly a
pointer.  Let Delphi handle the pointer math for you.

Secondly, do not use GetMem to instantiate a class.  GetMem only reserves
memory.  It does not initialise the object, eg. it does not set the VMT
pointer.

So to summarise, what you should have done is:

type
  TMain = class
  private
    connect: array[1..maxNum] of TMain;
    ...
  end;

procedure Button1Click
var
 M: TMain;
begin
 M := TMain.Create;
 M.Init;
end;

Hope this helps.

----- Original Message -----
From: "Gajo Istvan" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, December 22, 2003 1:48 AM
Subject: [DUG] Error with pointers


> 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;
>
>
> _______________________________________________
> Delphi mailing list
> [EMAIL PROTECTED]
> http://ns3.123.co.nz/mailman/listinfo/delphi
>
_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to