[DUG] Invalid Pointer Operation
David Brennan
dugdavid at dbsolutions.co.nz
Mon Aug 8 14:44:14 NZST 2005
SetLength(X, 10) creates a dynamic array from 0 to 9. You are referencing
items 1 to 10. That will be the problem I imagine.
David.
-----Original Message-----
From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz] On
Behalf Of Rohit Gupta
Sent: Monday, 8 August 2005 2:33 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] Invalid Pointer Operation
I am very confused here. Using D2005. The following is on a form
with two buttons - btnCreate and btnDestroy. When Destroy is
pressed I get an "Invalid Pointer Operation".
Seeing that this is the first time I have used open arrays (I detest
the concept) and setlength I must have done something wrong
here. But it escapes me.
The problem only shows up when I nest a TObject inside another.
unit Unit1;
{$d+,y+,l+,o-}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms,
Dialogs, StdCtrls;
type
TTest = class (TObject)
private
FMemory : array of integer;
public
constructor Create; reintroduce;
destructor Destroy; override;
end;
TTest2 = class (TObject)
private
FTests : array of TTest;
public
constructor Create; reintroduce;
destructor Destroy; override;
end;
type
TForm1 = class(TForm)
btnCreate: TButton;
btnDestroy: TButton;
procedure btnDestroyClick(Sender: TObject);
procedure btnCreateClick(Sender: TObject);
private
public
Test : TTest2;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TTest }
constructor TTest.Create;
begin
inherited Create;
Setlength (FMemory,10);
end;
destructor TTest.Destroy;
begin
FMemory := nil;
inherited;
end;
{ TTest2 }
constructor TTest2.Create;
var
I : Integer;
begin
inherited Create;
Setlength (FTests,10);
for I := 1 to 10
do FTests [I] := TTest.Create;
end;
destructor TTest2.Destroy;
var
I : Integer;
begin
for I := 1 to 10
do FTests [I].Free;
FTests := nil;
inherited;
end;
procedure TForm1.btnCreateClick(Sender: TObject);
begin
if not assigned (Test)
then Test := TTest2.Create;
end;
procedure TForm1.btnDestroyClick(Sender: TObject);
begin
Test.Free;
Test := nil;
end;
end.
Regards
Rohit
====================================================
==================
CFL - Computer Fanatics Ltd. 21 Barry's Point Road, AKL, New
Zealand
PH (649) 489-2280
FX (649) 489-2290
email rohit at cfl.co.nz or r.gupta at xtra.co.nz
====================================================
==================
_______________________________________________
Delphi mailing list
Delphi at ns3.123.co.nz
http://ns3.123.co.nz/mailman/listinfo/delphi
More information about the Delphi
mailing list